测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年10月 |
SwiftSwift 版本 | 4.0 |
SPM支持 SPM | ✓ |
由 Miguel Angel Quinones 维护。
SwiftAnimations
是一个小的 DSL,可以在 UIKit 上进行动画链操作,统一所有应用程序中的动画参数,并进一步简化动画代码。
将复杂的动画序列转换为简单的级联,易于阅读的代码
// Start with a call to 'animate'
animate {
self.red.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
}.thenAnimate { // Chain animations, to be executed in order
self.green.transform = CGAffineTransformMakeRotation(-CGFloat(M_PI_2))
}.afterDelay(1)
.thenAnimate {
self.blue.transform = CGAffineTransformMakeScale(1.5, 1)
}.withOptions(.CurveEaseOut).thenAnimate {
self.yellow.transform = CGAffineTransformMakeScale(1, 1.5)
}.withOptions(.CurveEaseOut).thenAnimate {
let scale = CGAffineTransformMakeScale(0.5, 0.5)
self.red.transform = scale
self.green.transform = scale
self.blue.transform = scale
self.yellow.transform = scale
}.completion { completed in
// Completion of all animations
println("Completed!")
}
SwiftAnimations
为所有动画配置默认值,如持续时间、曲线和弹簧阻尼和速度。您可以全局更改这些值或针对每个动画进行更改。
// All animations in my app have a baseline of 0.35. I rarely change this
setDefaultAnimationDuration(0.35)
// All animations in my app by default EaseIn EaseOut.
setDefaultAnimationCurve(.EaseInOut)
默认情况下,动画是“标准”动画,而不是弹簧动画。您可以通过切换简单的配置值来让所有动画更改使用的系统调用
// I want unified look across application, and I will be using spring animations everywhere unless otherwise specified
setDefaultAnimationType(.Spring)
// Moreover, tedious repetition removed by specifying a default value for damping and velocity
setDefaultSpringDamping(0.1)
setDefaultInitialVelocity(0.3)
如果未指定默认值,库将使用默认值。
您也可以更改每个动画的特定值
animate {
// Animation 1
}.withOptions(.CurveEaseOut).withDuration(0.5).thenAnimate { // Modify how 'previous' animation is performed
// Animation 2
}.withDuration(0.1).withType(.Spring).thenAnimate {
// Animation 3
}.withType(.Regular)
.afterDelay(0.2)
将来我会添加重新定义的动画。也许还可以配置所有应用程序代码中的这些动画。
animate(flip(self.avatarView))
.thenAnimate(bounce(self.button)).start()
欢迎提出建议和拉取请求 :)
UIKit 动画 API 设计得非常简单。它非常适合一次性动画。但有时应用程序需要更复杂的动画。
在许多情况下,需要将两个或三个动画链式调用以达到所需的视觉效果。通常,这可以通过嵌套UIKit.animateWithDuration(…)调用轻松实现,例如:
UIView.animateWithDuration(0.3, animations: {
//AnimationA
}, completion: {
UIView.animateWithDuration(0.3, animations: {
//AnimationB
}, completion: {
UIView.animateWithDuration(0.3, animations: {
//AnimationC
}, completion: {
UIView.animateWithDuration(0.3, animations: {
//AnimationD
}, completion: {
//Usual cleanup
})
})
})
})
想象一下在Objective-C中它是如何实现的。
这种方法存在一个很大的问题,那就是可读性。Swift可以通过将动画抽象到当前函数的内联函数中,使其稍微不那么痛苦,但我认为这仍然是很糟糕的代码。我相信动画和UI代码应该是清晰易读的,这样您就可以不用运行代码就能看到哪些元素会移动,以及移动到何处。
只需将源目录下的文件拖入您的项目。
Miguel Angel Quiñones / @miguelquinon
这个想法来源于我在使用Swift的当前项目中工作,并且看到了Spring。