iOS动画(一)----UIView动画之api认识

2018-08-01  本文已影响0人  雨_田

做了很久的伸手党,开始写点东西。由于工作中一直用objective-c编程,swift不常用,就从oc开始吧。UIView动画主要介绍以animate开头的block类方法展示不同的动画效果。
先当翻译官,介绍主要方法和枚举:
一:一个动画类方法,实现常规动画效果

/**创建一个动画
 *UIView普通动画各参数介绍
 *@duration:动画时长
 *@delay:动画延迟
 *@dampingRatio:动画阻尼 如果为1动画则平稳减速动画没有振荡。 这里值为 0~1
 *@velocity动画速率。数值越小,动力越小,弹簧的拉伸幅度就越小度 像素/秒
 *@options:动画效果枚举
 *
 *回调一:动画结束后,视图的状态
 *回调二:动画执行完毕
 */
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion ;

动画效果UIViewAnimationOptions

typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {

// 1.常规动画属性设置(可以同时选择多个进行设置)
UIViewAnimationOptionLayoutSubviews            = 1 <<  0,//动画过程中保证子视图跟随运动。即提交UIView动画时,自动更新Subview的Layout约束
UIViewAnimationOptionAllowUserInteraction      = 1 <<  1,//动画过程中允许用户交互,比如触摸
UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, //所有视图从当前状态开始运行
UIViewAnimationOptionRepeat                    = 1 <<  3,//动画无限重复
UIViewAnimationOptionAutoreverse               = 1 <<  4, //动画运行到结束点后仍然以动画方式回到初始点。**执行动画回路,前提是设置动画无限重复**
UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, //忽略嵌套动画时间设置。**忽略外层动画嵌套的时间变化曲线**
UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, //忽略嵌套动画速度设置。**通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照**
UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7,//动画过程中重绘视图(注意仅仅适用于转场动画)。
UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, //视图切换时直接隐藏旧视图、显示新视图,而不是将旧视图从父视图移除(仅仅适用于转场动画)**用显隐的方式替代添加移除图层的动画效果**
UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9,//不继承父动画设置或动画类型。**忽略嵌套继承的option选项**

//2.动画速度控制(可从其中选择一个设置)时间函数曲线相关**时间曲线函数**
UIViewAnimationOptionCurveEaseInOut            = 0 << 16,//动画先缓慢,然后逐渐加速,**默认**
UIViewAnimationOptionCurveEaseIn               = 1 << 16,//动画逐渐变慢
UIViewAnimationOptionCurveEaseOut              = 2 << 16,//动画逐渐加速
UIViewAnimationOptionCurveLinear               = 3 << 16,//动画匀速执行,默认值

//3.转场类型(仅适用于转场动画设置,可以从中选择一个进行设置,基本动画、关键帧动画不需要设置)**转场动画相关的**
//[UIView transitionFromView: toView: duration: options:  completion:^(****BOOL****finished) {}];
// 该方法效果是插入一面视图移除一面视图,期间可以使用一些转场动画效果。
UIViewAnimationOptionTransitionNone            = 0 << 20,//没有转场动画效果**默认**
UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,//从左侧翻转效果
UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,//从右侧翻转效果
UIViewAnimationOptionTransitionCurlUp          = 3 << 20,//向后翻页的动画过渡效果
UIViewAnimationOptionTransitionCurlDown        = 4 << 20,//向前翻页的动画过渡效果
UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,//旧视图溶解消失显示下一个新视图的效果
UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,//从上方翻转效果
UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,//底部翻转效果。

//4、帧率控制
UIViewAnimationOptionPreferredFramesPerSecondDefault     = 0 << 24,//默认的帧每秒.
UIViewAnimationOptionPreferredFramesPerSecond60          = 3 << 24,//60帧每秒的帧速率
UIViewAnimationOptionPreferredFramesPerSecond30          = 7 << 24,//30帧每秒的帧速率
} ;

其他方法:

/** 只控制  动画时长 动画延迟 动画效果枚举; 保留双回调*/
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

/** 只控制  动画时长; 保留双回调*/
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0

/** 只控制  动画时长; 保‘动画结束后,视图的状态’回调*/
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL

/** 过渡动画 */
/** 当一个视图view的内容需要变化时,比如删除或增加子视图,可以使用过渡动画 */
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
/** 在动画过程中,首先将 fromView 从父视图中删除,然后将 toView 添加,就是做了一个替换操作 */
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ __nullable)(BOOL finished))completion); 

/** 在一组视图上执行指定的系统动画,并可以并行自定义的动画*/
/** 其中parallelAnimations就是与系统动画并行的自定义动画 */
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindof UIView *> *)views options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))parallelAnimations completion:(void (^ __nullable)(BOOL finished))completion;

二、一个关键帧动画类方法,可以使用它来创建更多更复杂更酷炫的动画效果,而不需要去使用到核心动画(CoreAnimation)

/**创建一个关键帧动画
  *UIView关键帧动画各参数介绍
  *@duration:动画时长
  *@delay:动画延迟
  *@dampingRatio:阻尼,取值0~1
  *@velocity:速度 像素/秒
  *@options:动画效果枚举
  *
  *回调一:动画结束后,视图的到达状态
  *回调二:动画执行完毕
  */
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;

关键帧动画效果UIViewKeyframeAnimationOptions

typedef NS_OPTIONS(NSUInteger, UIViewKeyframeAnimationOptions) {
UIViewKeyframeAnimationOptionLayoutSubviews            = UIViewAnimationOptionLayoutSubviews,///动画过程中保证子视图跟随运动。即提交UIView动画时,自动更新Subview的Layout约束
UIViewKeyframeAnimationOptionAllowUserInteraction      = UIViewAnimationOptionAllowUserInteraction, //动画过程中允许用户交互,比如触摸
UIViewKeyframeAnimationOptionBeginFromCurrentState     = UIViewAnimationOptionBeginFromCurrentState,//所有视图从当前状态开始运行
UIViewKeyframeAnimationOptionRepeat                    = UIViewAnimationOptionRepeat, //动画无限重复
UIViewKeyframeAnimationOptionAutoreverse               = UIViewAnimationOptionAutoreverse, //动画运行到结束点后仍然以动画方式回到初始点。**执行动画回路,前提是设置动画无限重复**
UIViewKeyframeAnimationOptionOverrideInheritedDuration = UIViewAnimationOptionOverrideInheritedDuration, //忽略嵌套动画时间设置。**忽略外层动画嵌套的时间变化曲线**
UIViewKeyframeAnimationOptionOverrideInheritedOptions  = UIViewAnimationOptionOverrideInheritedOptions, ////不继承父动画设置或动画类型。**忽略嵌套继承的option选项**


UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 10, // 连续运算模式,线性  **选择使用一个简单的线性插值计算的时候关键帧之间的值,默认*
UIViewKeyframeAnimationOptionCalculationModeDiscrete   = 1 << 10,// 离散运算模式,只显示关键帧  **选择不插入关键帧之间的值,而是直接跳到每个新的关键帧的值*
UIViewKeyframeAnimationOptionCalculationModePaced      = 2 << 10,// 均匀执行运算模式,线性  **选择计算中间帧数值算法使用一个简单的节奏。这个选项的结果在一个均匀的动画*
UIViewKeyframeAnimationOptionCalculationModeCubic      = 3 << 10,//平滑运算模式  **选择计算中间帧使用默认卡特莫尔罗花键,通过关键帧的值。你不能调整该算法的参数。 这个动画好像会更圆滑一些*
UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10//平滑均匀运算模式 **选择计算中间帧使用立方计划而忽略的时间属性动画。相反,时间参数计算隐式地给动画一个恒定的速度*
} ;

其他方法:

/**在动画的代码块中插入关键帧动画信息
  *frameStartTime:表示关键帧动画开始的时刻在整个动画中的百分比
 *frameDuration:表示这个关键帧动画占用整个动画时长的百分比
 */
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations; 
上一篇下一篇

猜你喜欢

热点阅读