Home

Awesome

TheAnimation

<p align="center"> <img src="http://img.shields.io/badge/platform-iOS | tvOS | macOS-blue.svg?style=flat" alt="Platform" /> <a href="https://developer.apple.com/swift"> <img src="http://img.shields.io/badge/Swift-4.1%20|%204.2%20|%205.0%20|%205.1-brightgreen.svg?style=flat" alt="Language"> </a> <a href="https://github.com/Carthage/Carthage"> <img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage"> </a> <a href="https://cocoapods.org/pods/TheAnimation"> <img src="https://img.shields.io/cocoapods/v/TheAnimation.svg?style=flat" alt="Version"> </a> <a href="https://cocoapods.org/pods/TheAnimation"> <img src="https://img.shields.io/cocoapods/l/TheAnimation.svg?style=flat" alt="License"> </a> <a href="https://travis-ci.org/marty-suzuki/TheAnimation"> <img src="https://img.shields.io/travis/marty-suzuki/TheAnimation.svg?style=flat" alt="CI Status"> </a> </p>

TheAnimation is Type-safe CAAnimation wrapper.

Introduction

For example, if you want to animate backgroundColor with CABasicAnimation, you need to consider type because fromValue property and so on are Any?.

If you use BasicAnimation of TheAnimation, you can animate backgroundColor without considering type! (AnimationKeyPaths.backgroundColor is AnimationKeyPath<CGColor> type.)

Usage

The way of making an animation is almost similar CAAnimation. But you need to use animation.animate(in:) method instead of using layer.add(_:forKey:).

let view = UIView()

let animation = BasicAnimation(keyPath: .opacity)
animation.fromValue = 0
animation.toValue   = 1
animation.duration  = 1
animation.animate(in: view)

animation.animate(in:) returns AnimaitonCanceller. You can cancel an animation with it.

let canceller = animation.animate(in: view)
canceller.cancelAnimation()

Example

To run the example project, clone the repo, and open Example directory.

Correspondence Table

CAAnimationTheAnimation
CAPropertyAnimationPropertyAnimation
CABasicAnimationBasicAnimation
CAKeyframeAnimationKeyframeAnimation
CASpringAnimationSpringAnimation
CATransitionTransitionAnimation
CAAnimationGroupAnimationGroup

Add new AnimationKeyPath

You can add AnimationKeyPath like this.

extension AnimationKeyPaths {
    static let newKeyPath = AnimationKeyPath<CGFloat>(keyPath: "abcd")
}

Handle animation did Start / Stop

You can handle animation did Start with func setAnimationDidStart(handler:). In addition, you can handle animation did Stop with func setAnimationDidStop(handler:).

let view = UIView()

let animation = BasicAnimation(keyPath: .opacity)
animation.fromValue = 0
animation.toValue   = 1
animation.duration  = 1

animation.setAnimationDidStart {
    // do something
}

animation.setAnimationDidStop { finished in
    // do something
}

animation.animate(in: view)

Requirements

Installation

Carthage

If you’re using Carthage, simply add TheAnimation to your Cartfile:

github "marty-suzuki/TheAnimation"

CocoaPods

TheAnimation is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'TheAnimation'

Author

marty-suzuki, s1180183@gmail.com

License

TheAnimation is available under the MIT license. See the LICENSE file for more info.