anim
是一个以简单、声明性 API 为目标的 Swift 编写的动画库。
// moves box to 100,100 with default settings
anim {
self.box.frame.origin = CGPoint(x:100, y:100)
}
// after that, waits 100 ms
.wait(0.1)
// moves box to 0,0 after waiting
.then {
self.box.frame.origin = CGPoint(x:0, y:0)
}
// displays message after all animations are done
.callback {
print("Just finished moving 📦 around.")
}
它支持大量的缓动函数和链式动画。它是 Apple 的 UIViewPropertyAnimator
的包装,并在 iOS 和 tvOS 10 之前版本回退到 UIView.animate
。它在 macOS 上使用 NSAnimationContext
。
示例
示例项目可在 examples/
文件夹和 XCode 项目的目标中找到。
Cocoapods
pod 'anim'
Carthage
github "onurersel/anim"
手动
或者简单地将 src/
文件夹中的 swift 文件拖入您的项目。
有关完整文档,请访问 http://onurersel.github.io/anim/。
使用 anim
构造函数初始化动画。
// Initialize with default settings
anim {
// animation block
}
// or initialize with it's own settings
anim { (settings) -> (animClosure) in
settings.delay = 1
settings.duration = 0.7
settings.ease = .easeInOutBack
return {
// animation block
}
}
// or initialize layout constraint animations just by passing the parent view
anim(constraintParent: self.view) {
// animation block
}
anim(constraintParent: self.view) { (settings) -> (animClosure) in
// settings...
return {
// animation block
}
}
// you don't need to call layoutIfNeeded() before or inside the
// animation blocks, it's handled by anim
//
// for example to update constant value of a constraint,
// you can just change it inside the animation block
let width: NSLayoutConstraint //...
anim(constraintParent: self.view) {
width.constant = 100 // new value
}
// that's it!
使用then
函数进行链式动画。
anim {}
.then{
// next animation block
}
anim {}
.then { (settings) -> animClosure in
settings.duration = 1
return {
// next animation block
}
}
anim {}
.then(constraintParent: self.view) {
// chaining constraint animations
}
.then(constraintParent: self.view) { (settings) -> animClosure in
settings.duration = 1
return {
// next animation block for constraints
}
}
使用wait
函数在动画步骤之间等待。
anim{}.wait(0.25).then{} //...
使用.callback
函数在动画步骤之间插入回调。
anim{}
.callback {
// custom block
}
.then{} //...
使用stop
函数停止动画。
let animation = anim{}.then{} // ...
animation.stop()
默认设置
您可以通过anim.defaultSettings
属性更改默认动画设置。
anim.defaultSettings.ease = .easeInOutCubic
缓动效果
anim.Ease
提供了一个系列的缓动选项。
- 链式动画
- 等待,回调函数
- 约束动画
- 支持iOS 8和9
- 支持macOS
- 支持tvOS
- 示例项目
- 编排动画的API
- 分组动画
- 弹性缓动
- 形状动画
anim
在MIT许可下发布。有关详情,请参阅LICENSE。