一个框架,可以使用自定义曲线在任意属性上执行动画。
要求:Xcode 8、Swift 3.0
Animatable
协议的实例添加到列表中,以便它们将接收与显示刷新同步的 update
调用。Curve
类来简化您的代码。CompositeCurve
或通过实现 Parametric
协议来创建自己的曲线。您可以使用 Cocoapods 安装和使用此框架,或者将 Animation
项目手动添加到您的工作区并更新构建设置。
Animation.xcproject
项目包含到您的 Workspace 中。Animation.framework
集成/链接到您的目标中。导入模块
import Animation
执行一次性动画
let startPoint = CGPoint(x: 10, y: 10)
let endPoint = CGPoint(x: 100, y: 100)
let startColor = UIColor.black
let endColor = UIColor.red
Animation.animate(identifier: "example", duration: 0.5,
update: { (progress) -> Bool in
myView.center = startPoint <~~ Curve.easeInEaseOut[progress] ~~> endPoint
myView.backgroundColor = startColor <~~ progress ~~> endColor
return true
})
发生了什么
函数 Animation.animate(identifier:duration:update:completion:)
立即开始动画,以指定持续时间,反复调用您的更新代码(与显示刷新同步)。在更新闭包内,使用 UIView
实例更新其 center
和 backgroundColor
属性。您可以进行任何计算,并更新任何属性,包括那些通常无法使用 UIKit
动画进行动画化处理的属性。插值运算符用于获取起始值和结束值之间的插值值,例如 10.0 <~~ 0.1 ~~> 20.0
将返回从 10.0 到 20.0 的 0.1 的值,即 11.0。《code>progress 参数用于对 backgroundColor
执行线性插值,Curve.easeInEaseOut
对象用于将线性 progress
转换成缓入缓出曲线以动画化位置。
导入模块
import Animation
采纳 Animatable
协议
class MyAnimatableClass: Animatable {
遵从 Animatable
协议
func update(by timeInterval: Double) {
// Do whatever you want, using timeInterval to calculate the new position of things
}
将您的实例添加以接收更新
Animation.add(animatable: self)