TheAnimation
TheAnimation 是 Type-safe CAAnimation 包装器。
简介
例如,如果您想使用 CABasicAnimation 动画 backgroundColor
,由于 fromValue 属性等原因都是 Any?
类型,您需要考虑到类型问题。
如果您使用 BasicAnimation of TheAnimation
,则可以不考虑类型来动画化 backgroundColor
!(《AnimationKeyPaths.backgroundColor》的类型是 AnimationKeyPath
。)
使用方法
制作动画的方式几乎与 CAAnimation
相似。但是您需要使用 animation.animate(in:)
方法,而不是使用 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:)
返回 AnimaitonCanceller
。您可以使用它来取消动画。
let canceller = animation.animate(in: view)
canceller.cancelAnimation()
示例
要运行示例项目,请克隆仓库,然后打开 Example 目录。
通信表
CAAnimation | 动画 |
---|---|
CAPropertyAnimation | 属性动画 |
CABasicAnimation | 基础动画 |
CAKeyframeAnimation | 关键帧动画 |
CASpringAnimation | 弹簧动画 |
CATransition | 过渡动画 |
CAAnimationGroup | 动画组 |
AnimationKeyPath
添加新的 您可以像这样添加 AnimationKeyPath
。
extension AnimationKeyPaths {
static let newKeyPath = AnimationKeyPath<CGFloat>(keyPath: "abcd")
}
要求
- Xcode 9.3
- iOS 9 或以上
- tvOS 9 或以上
- macOS 10.11 或以上
- Swift 4.2(自 0.3.0 版本起)
安装
Carthage
如果您使用 Carthage,只需将 TheAnimation 添加到您的 Cartfile
github "marty-suzuki/TheAnimation"
CocoaPods
TheAnimation 可以通过 CocoaPods 安装。要安装它,只需将以下行添加到您的 Podfile 中
pod 'TheAnimation'
作者
marty-suzuki, [email protected]
许可证
TheAnimation 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。