NUAnimationKit 0.2.4.7

NUAnimationKit 0.2.4.7

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2016 年 7 月

Victor Maraccini 维护。



  • Victor Maraccini

UIView 动画包装器以简化动画命令的链式传递。

问题

链式传递 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 动画链

该库通过简单的语法创建了一个围绕 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);

例如设置字符串值

基于进度的 UIView 动画

NUAnimationKit 还支持使用简单的 UIView 动画块创建参数化动画

1 - 像往常一样构建您的块动画,但设置与您进行的转换相关联的视图

[self.controller addAnimations:^{
     [animationView1 setFrameY:400];
     animationView1.backgroundColor = [UIColor grayColor];
}].withAnimationOption(UIViewAnimationOptionTransitionCrossDissolve)
.withDuration(2)
.withAssociatedViews(@[animationView1]);

注意:您仍然可以添加尽可能多的动画块。每个块的顺序、持续时间和延迟将被保留。

2 - 在需要更改动画状态时调用进度更新块

[self.controller animateToProgress:progress];

注意事项

此动画方法不支持 beforeafter 块的链式传递。

安装

NUAnimationKit 可通过 CocoaPods 提供。要安装,只需将以下行添加到您的 Podfile 中

pod "NUAnimationKit"

用法

要使用此库,只需

  • 通过将 pod "NUAnimationKit" 添加到您的 Podfile 中来安装 pod
  • 导入 NUAnimationController.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 文件。