# iOS 交互、手势与动画编程(下)| 青训营笔记

110 阅读3分钟

这是我参与「第四届青训营 」笔记创作活动的的第14天

iOS 交互、手势与动画编程(下)| 青训营笔记

这次主要整理动画相关的笔记主要针对View动画,而Core Animation动画和Lottie动画暂未用到,暂不总结

动画踩坑

如果父视图使用layoutsubview更新view,动画不能做在view的最外层,使用layoutsubview会打断动画/取消动画,请参考


以下内容是课程中所学的View动画,这一部分概念并不复杂,关键字在于对API的灵活应用,以及一些动画拼接和组合的想象力。

View动画

Block动画

Block动画是日常开发中最经常使用的方法,用于完成一些基本的动画效果

  1. 位置:平移/旋转
  2. 显示:颜色/透明度变化
// 基础方法
[UIView animateWithDuration:动画时间 animations:^{动画变换内容}];
// 带执行后回调的方法
[UIView animateWithDuration:动画时间 animations:^{动画变换内容} completion:^(BOOL finished) { //动画执行提交后的操作 }];
// 带延时和过渡效果的方法
[UIView animateWithDuration:动画时间 delay:延迟时间 options:过渡效果 animations:^{动画变换内容} completion:^(BOOL finished) { //动画执行提交后的操作 }]; 

其中过渡效果的枚举值UIViewAnimationOptions有如下选项:

UIViewAnimationOptionLayoutSubviews //进行动画时布局子控件 
UIViewAnimationOptionAllowUserInteraction //进行动画时允许用户交互
UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画 
UIViewAnimationOptionRepeat //无限重复执行动画 
UIViewAnimationOptionAutoreverse //执行动画回路 
UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画的执行时间设置 
UIViewAnimationOptionOverrideInheritedCurve //忽略嵌套动画的曲线设置 
UIViewAnimationOptionAllowAnimatedContent //转场:进行动画时重绘视图 
UIViewAnimationOptionShowHideTransitionViews //转场:移除(添加和移除图层的)动画效果 
UIViewAnimationOptionOverrideInheritedOptions //不继承父动画设置 
UIViewAnimationOptionCurveEaseInOut //时间曲线,慢进慢出(默认值) 
UIViewAnimationOptionCurveEaseIn //时间曲线,慢进 
UIViewAnimationOptionCurveEaseOut //时间曲线,慢出 
UIViewAnimationOptionCurveLinear //时间曲线,匀速 
UIViewAnimationOptionTransitionNone //转场,不使用动画 
UIViewAnimationOptionTransitionFlipFromLeft //转场,从左向右旋转翻页 
UIViewAnimationOptionTransitionFlipFromRight //转场,从右向左旋转翻页 
UIViewAnimationOptionTransitionCurlUp //转场,下往上卷曲翻页 
UIViewAnimationOptionTransitionCurlDown //转场,从上往下卷曲翻页 
UIViewAnimationOptionTransitionCrossDissolve //转场,交叉消失和出现 
UIViewAnimationOptionTransitionFlipFromTop //转场,从上向下旋转翻页 
UIViewAnimationOptionTransitionFlipFromBottom //转场,从下向上旋转翻页

弹簧动画:iOS7.0新增

iOS原生拖拽等动画也经常使用,视觉上更灵动有活力

self.redView.alpha = 0.0;
[UIView animateWithDuration:(NSTimeInterval)  // 动画时间 
delay:(NSTimeInterval)  // 延迟时间 
usingSpringWithDamping:(CGFloat)  // 震动效果,范围0~1,数值越小震动效果越明显 
initialSpringVelocity:(CGFloat)  // 初始速度,数值越大初始速度越快 
options:(UIViewAnimationOptions)  // 动画的过渡效果 
animations:^{}  // 动画
completion:^(BOOL finished) {}];  //动画执行提交后的操作 

关键帧动画:IOS7.0新增

支持属性关键帧,但不支持路径关键帧

[UIView animateKeyframesWithDuration:(NSTimeInterval)// 动画时间 
delay:(NSTimeInterval) // 延迟时间 
options:(UIViewKeyframeAnimationOptions)//动画的过渡效果
animations:^{}// 动画
completion:^(BOOL finished) {}];//动画执行提交后的操作 

动画的过渡效果枚举值UIViewKeyframeAnimationOptions

UIViewAnimationOptionLayoutSubviews //进行动画时布局子控件
UIViewAnimationOptionAllowUserInteraction //进行动画时允许用户交互 
UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画 
UIViewAnimationOptionRepeat //无限重复执行动画 
UIViewAnimationOptionAutoreverse //执行动画回路 
UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画的执行时间设置 
UIViewAnimationOptionOverrideInheritedOptions //不继承父动画设置
UIViewKeyframeAnimationOptionCalculationModeLinear //运算模式 :连续 
UIViewKeyframeAnimationOptionCalculationModeDiscrete //运算模式 :离散
UIViewKeyframeAnimationOptionCalculationModePaced //运算模式 :均匀执行
UIViewKeyframeAnimationOptionCalculationModeCubic //运算模式 :平滑 
UIViewKeyframeAnimationOptionCalculationModeCubicPaced //运算模式 :平滑均匀

增加属性关键帧

[UIView addKeyframeWithRelativeStartTime:(double)//动画开始的时间(占总时间的比例)
relativeDuration:(double) //动画持续时间(占总时间的比例,0-1)
animations:^{}];

转场动画

单视图:通过单个view移入或移出实现转场

[UIView transitionWithView: 
duration: 
options:
animations:
completion:];

双视图:本质仍是通过对单个视图实施动画效果,只是首先将fromView从父视图移出,并将toView添加到父视图,但要注意转场动画的作用对象是父视图(过渡效果体现在父视图上)

[UIView transitionFromView:(nonnull UIView *) 
toView:(nonnull UIView *) 
duration:(NSTimeInterval) 
options:(UIViewAnimationOptions) 
completion:^(BOOL finished) {}];
//等价于
[fromView.superview addSubview:toView];
[fromView removeFromSuperview];