Anima
Anima 是 Swift5 的链式基于层动画库。
它支持更容易地制作序列和分组动画。
如下所示编写。
let startAnimations: [AnimaType] = [.moveByY(-50), .rotateByZDegree(90)]
let moveAnimations: [AnimaType] = [.moveByX(50), .rotateByZDegree(90)]
let endAnimations: [AnimaType] = [.moveByY(-50), .rotateByZDegree(90)]
animaView.layer.anima
.then(.opacity(1.0))
.then(group: startAnimations)
.then(group: moveAnimations, options: labelAnimaOption(index: 0))
.then(group: moveAnimations, options: labelAnimaOption(index: 1))
.then(group: moveAnimations, options: labelAnimaOption(index: 2))
.then(group: moveAnimations, options: labelAnimaOption(index: 3))
.then(group: endAnimations, options: labelAnimaOption(index: 4))
.then(group: [.scaleBy(0.0), .opacity(0.0)])
func labelAnimaOption(index: Int) -> [AnimaOption] {
let labelAnima = labels[index]?.layer.anima
return [.completion({
labelAnima?.then(.opacity(1)).fire()
})]
}要求
Anima 需要 Swift4 以及更高版本和 iOS9.0 以上版本
特性
- 实现了来自 easings.set 的几乎所有时序模式。
- 弹力动画(以 CASpringAnimation 为特色)
- 类型安全的动画键路径 ()
用法
移动位置
如果您想相对地转换CALayer.position,请使用.moveByX(CGFloat)、.moveByY(CGFloat)或.moveByXY(x: CGFloat, y: CGFloat)动画类型。
layer.anima.then(.moveByX(50)).fire()或如果已确定目标,请使用.moveTo(x: CGFloat, y: CGFloat)。
※ Anima在动画期间不会更新CALayer.position值。因为当更新基于层的视图的层位置值时,它经常被重置为默认值。
顺序动画
Anima支持。
分组动画
要并发运行动画,请使用CoreAnimation的CAAnimationGroup。在Anima中,您可以使用Anima.then(group: )来并发运行一些AnimaType。
以下是如何并发运行移动、缩放和旋转动画的示例。
layer.anima
.then(group: [.moveByX(200),
.scaleBy(1.5),
.rotateByZDegree(180)])
.fire()动画选项
Anima有一些选项。
- 持续时间(TimeInterval)
- 时序函数(TimingFunction) - 修改定义动画节奏的时序函数。 - 默认时序函数位于
Anima.defaultTimingFunction。如果您没有设置时序函数选项,则使用默认时序函数。*请参阅Anima.TimingFunction.swift - 重复次数(count: Float) - 要无限运行动画,请设置为
.infinity。 - 自动反向
- 完成回调(() -> Void)
您可以使用以下值。
layer
.anima
.then(.moveByX(100), options: [.autoreverse,
.timingFunciton(.easeInQuad),
.repeat(count: 3),
.completion({
print("completion")
})])
.fire()
旋转动画 & 锚点
AnimaType具有3种旋转动画类型:.rotateByX、.rotateByY、.rotateByZ。每种动画类型都有两种值类型,分别为degrees和radians。您使用任意的类型。
CALayer具有AnchorPoint属性。旋转、移动或其他动画都会受其影响。默认值是(0.5, 0.5)。使用AnimaType.moveAnchor(AnimaAnchorPoint)可以移动层的AnchorPoint。
layer.anima
.then(.rotateByZDegree(360))
.then(.moveAnchor(.topLeft))
.then(.rotateByZDegree(360))
.fire()如果你只想修改AnchorPoint,请使用Anima.then(setAnchor: AnimaAnchorPoint)。
layer.anima
.then(.rotateByZDegree(360))
.then(setAnchor: .topLeft)
.then(.rotateByZDegree(360))
.fire()移动路径
如果你想让移动动画更加复杂,请使用.movePath(path: CGPath, keyTymes: [Double])。Anima示例app有一个通过拖动手势创建动画的示例,你可以看看!
但是当你与AnimaOption.autoreverse一起使用时,可能会出现任何问题。所以如果你使用它,请注意选项。
原始KeyPath
如果你想对其他可动值进行动画处理,可以使用AnimaType.original(keyPath: String, from: Any?, to: Any)。例如,CAEmitterLayer的动画就是这样。
let layer = CAEmitterLayer()
layer.emitterPosition = CGPoint(x: 100.0, y:100.0)
layer.anima
.then(.original(keyPath: #keyPath(CAEmitterLayer.emitterPosition), from: layer.emitterPosition, to: CGPoint(x: 200.0, y:200.0)))
.fire()示例
要运行示例项目,请克隆库,打开Anima.xcodeproj,并运行目标Anima iOS Example。
安装
Cocoapods
可以通过CocoaPods来获取Anima。要安装它,只需在你的Podfile中添加以下行:
pod "Anima"
# If you want to use Swift 3 version, Please specify Anima version.
pod "Anima", "0.5.1"Carthage
将github satoshin21/Anima 添加到您的 Cartfile 中。执行 carthage update 以安装。
作者
Satoshi Nagasaka, [email protected]
许可协议
Anima 可在 MIT 许可协议下使用。有关更多信息,请参阅 LICENSE 文件。





