链式传递 UIView 动画需要使用完成处理程序块来链接命令
[UIView animateWithDuration:0.5
delay:0
options:0
animations:^{
[animationView1 setFrameY:400];
animationView1.backgroundColor = [UIColor grayColor];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.3
delay:0.1
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[animationView2 setFrameY:400];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:0
animations:^{
[animationView3 setFrameY:400];
}
completion:nil];
}];
}];
这不是一个特别优雅的解决方案,并且难以阅读。
该库通过简单的语法创建了一个围绕 UIView 动画的包装器,从而简化了链式传递
NUAnimationController *controller = [[NUAnimationController alloc] init];
[self.controller addAnimation:^{
[animationView1 setFrameY:400];
animationView1.backgroundColor = [UIColor grayColor];
}].withAnimationOption(UIViewAnimationOptionTransitionCrossDissolve);
[self.controller addAnimation:^{
[animationView2 setFrameY:400];
}].withDelay(0.1).withDuration(0.3).withCurve(UIViewAnimationCurveEaseInOut);
[self.controller addAnimation:^{
[animationView3 setFrameY:400];
}].withType(NUAnimationTypeSpringy).withDuration(NUSpringAnimationNaturalDuration);
以在不同的对象上实现链式传递
NUAnimationKit 还允许您在创建时设置动画选项
[controller addAnimation:^{
//Animations
}].withDelay(0.1).withDuration(0.3).withCurve(UIViewAnimationCurveEaseInOut);
以及基于弹力的动画
controller addAnimation:^{
//Springy
}].withType(NUAnimationTypeSpringy).withDuration(NUSpringAnimationNaturalDuration)
其中 NUSpringAnimationNaturalDuration
是一个常数,它会根据物理属性(如 质量 和 弹性常数)自动计算最佳弹力动画持续时间。
如下所示
它还增加了对基于进度的块的支持,用于无法直接动画化的属性。
它们可以与另一个 UIView 动画一起动画化
[controller addAnimation:^{
//Main animation
}].alongSideBlock(^(CGFloat progress){
progressLabel.text = [NSString stringWithFormat:@"%f", progress];
});
或者完全独立创建
[self.controller addProgressAnimations:^(CGFloat progress) {
progressLabel.text = [NSString stringWithFormat:@"%f", progress];
}].withDuration(2);
例如设置字符串值
NUAnimationKit 还支持使用简单的 UIView 动画块创建参数化动画
1 - 像往常一样构建您的块动画,但设置与您进行的转换相关联的视图
[self.controller addAnimations:^{
[animationView1 setFrameY:400];
animationView1.backgroundColor = [UIColor grayColor];
}].withAnimationOption(UIViewAnimationOptionTransitionCrossDissolve)
.withDuration(2)
.withAssociatedViews(@[animationView1]);
注意:您仍然可以添加尽可能多的动画块。每个块的顺序、持续时间和延迟将被保留。
2 - 在需要更改动画状态时调用进度更新块
[self.controller animateToProgress:progress];
此动画方法不支持 before
和 after
块的链式传递。
NUAnimationKit 可通过 CocoaPods 提供。要安装,只需将以下行添加到您的 Podfile 中
pod "NUAnimationKit"
要使用此库,只需
pod "NUAnimationKit"
添加到您的 Podfile 中来安装 podNUAnimationController.h
NUAnimationController *controller = [[NUAnimationController alloc] init];
[controller addAnimation:^{
//Springy
}].withType(NUAnimationTypeSpringy).withDuration(NUSpringAnimationNaturalDuration)
[controller startAnimationChain]
或者
[controller startAnimationChainWithCompletionBlock:^{
//Optional completion block
}];
适配 iOS 8.0 及以上版本。
Victor Maraccini, [email protected]
NUAnimationKit 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。