UIView的动画块代码
block 的方法主要有 7 个
这个是用的最多的 ( 主要在于参数的和回调)
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^ __nullable)(BOOLfinished))completion ;[UIViewanimateWithDuration:(NSTimeInterval)// 动画的持续时间delay:(NSTimeInterval)// 动画执行的延迟时间options:(UIViewAnimationOptions)// 执行的动画选项,animations:^{// 要执行的动画代码} completion:^(BOOLfinished) {// 动画执行完毕后的调用}];
和上一个方法相比较,少了动画延迟和动画选项 (使用的复杂度比上一个小)
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animations completion:(void(^ __nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0);// delay = 0.0, options = 0[UIViewanimateWithDuration:(NSTimeInterval) animations:^{// 要执行的动画代码} completion:^(BOOLfinished) {// 动画执行完毕后的调用}];
这个是最简单的,对动画的设置基本没有,使用的都是默认参数
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animationsNS_AVAILABLE_IOS(4_0);// delay = 0.0, options = 0, completion = NULL[UIViewanimateWithDuration:(NSTimeInterval) animations:^{ }];
Spring Animationring Animation
在IOS7开始,系统动画效果广泛应用Spring Animation
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^ __nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(7_0); [UIViewanimateWithDuration:(NSTimeInterval)// 动画持续时间delay:(NSTimeInterval)// 动画延迟时间usingSpringWithDamping:(CGFloat)// 类似弹簧振动效果 0~1initialSpringVelocity:(CGFloat)// 初始速度options:(UIViewAnimationOptions)// 动画过渡效果animations:^{// code} completion:^(BOOLfinished) {// code}]usingSpringWithDamping:它的范围为0.0f 到1.0f ,数值越小「弹簧」的振动效果越明显。initialSpringVelocity:初始的速度,数值越大一开始移动越快。值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。Spring Animation 是线性动画或 ease-out动画的理想替代品。由于 iOS 本身大量使用的就是 Spring Animation,用户已经习惯了这种动画效果,因此使用它能使 App 让人感觉更加自然,用 Apple 的话说就是「instantly familiar」。此外,Spring Animation 不只能对位置使用,它适用于所有可被添加动画效果的属性。
+ (void)transitionWithView:(UIView*)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void(^ __nullable)(void))animations completion:(void(^ __nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0); [UIViewtransitionWithView:(nonnullUIView*) duration:(NSTimeInterval) options:(UIViewAnimationOptions)options animations:^{ code } completion:^(BOOLfinished) { code }];
+ (void)transitionFromView:(UIView*)fromView toView:(UIView*)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void(^ __nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(4_0);// toView added to fromView.superview, fromView removed from its superview[UIViewtransitionFromView:(nonnullUIView*) toView:(nonnullUIView*) duration:(NSTimeInterval) options:(UIViewAnimationOptions) completion:^(BOOLfinished) { code }];
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindofUIView*> *)views options:(UIViewAnimationOptions)options animations:(void(^ __nullable)(void))parallelAnimations completion:(void(^ __nullable)(BOOLfinished))completionNS_AVAILABLE_IOS(7_0);[UIViewperformSystemAnimation:<(UISystemAnimation)> onViews:(nonnullNSArray<__kindofUIView*> *) options:<(UIViewAnimationOptions)> animations:^{ code} completion:^(BOOLfinished) { code}]
三、关键帧动画
UIView动画已经具备高级的方法来创建动画,而且可以更好地理解和构建动画。
IOS7以后苹果新加了一个animateKeyframesWithDuration的方法,我们可以使用它来创建更多更复杂更酷炫的动画效果,而不需要去使用到核心动画(CoreAnimatino)。
/** * 添加关键帧方法 * *@paramduration 动画时长 *@paramdelay 动画延迟 *@paramoptions 动画效果选项 *@paramanimations 动画执行代码 *@paramcompletion 动画结束执行代码 */+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void(^)(void))animations completion:(void(^)(BOOL finished))completion;