核心动画
2015-07-18 07:53
351 查看
核心动画
标签(空格分隔): ios进阶Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍。也就是说,使用少量的代码就可以实现非常强大的功能。
Core Animation是直接作用在CALayer上的,并非UIView
核心动画的框架结构
Core Animation的使用步骤
如果不是xcode5之后的版本,使用它需要先添加QuartzCore.framework和引入对应的框架
<QuartzCore/QuartzCore.h>
开发步骤:
1. 首先得有CALayer
2. 初始化一个CAAnimation对象,并设置一些动画相关属性
3. 通过调用
CALayer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始执行动画了
4. 通过调用CALayer的
removeAnimationForKey:方法可以停止CALayer中的动画
CAAnimation
是所有动画对象的父类,负责控制动画的持续时间和速度,是个抽象类,不能直接使用,应该使用它具体的子类属性(一些是来自CAMediaTiming协议的属性)
duration:动画的持续时间
repeatCount:重复次数,无限循环可以设置HUGE_VALF或者MAXFLOAT
repeatDuration:重复时间
removedOnCompletion:默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwards
fillMode:决定当前对象在非active时间段的行为。比如动画开始之前或者动画结束之后
beginTime:可以用来设置动画延迟执行时间,若想延迟2s,就设置为CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间
timingFunction:速度控制函数,控制动画运行的节奏
delegate:动画代理
动画填充模式
kCAFillModeRemoved
这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
kCAFillModeForwards
当动画结束后,layer会一直保持着动画最后的状态
kCAFillModeBackwards在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
kCAFillModeBoth
这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
速度控制函数(CAMediaTimingFunction)
kCAMediaTimingFunctionLinear(线性):匀速,给你一个相对静态的感觉
kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
kCAMediaTimingFunctionEaseInEaseOut(渐进渐出):动画缓慢的进入,中间加速,然后减速的到达目的地。这个是默认的动画行为。
CAAnimationDelegate(动画代理方法)
- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
CALayer上动画的暂停和恢复
#pragma mark 暂停CALayer的动画 -(void)pauseLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; // 让CALayer的时间停止走动 layer.speed = 0.0; // 让CALayer的时间停留在pausedTime这个时刻 layer.timeOffset = pausedTime; }
#pragma mark 恢复CALayer的动画 -(void)resumeLayer:(CALayer*)layer { CFTimeInterval pausedTime = layer.timeOffset; // 1. 让CALayer的时间继续行走 layer.speed = 1.0; // 2. 取消上次记录的停留时刻 layer.timeOffset = 0.0; // 3. 取消上次设置的时间 layer.beginTime = 0.0; // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime) CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause) layer.beginTime = timeSincePause;
CAPropertyAnimation
CAPropertyAnimation是CAAnimation的子类,也是个抽象类,要想创建动画对象,应该使用它的两个子类:CABasicAnimation,
CAKeyframeAnimation
keyPath:通过指定CALayer的一个属性名称为keyPath(NSString类型),并且对CALayer的这个属性的值进行修改,达到相应的动画效果。比如,指定@“position”为keyPath,就修改CALayer的position属性的值,以达到平移的动画效果
CABasicAnimation
属性:
fromValue:keyPath相应属性的初始值toValue:keyPath相应属性的结束值
动画过程说明:
随着动画的进行,在长度为duration的持续时间内,keyPath相应属性的fromValue渐渐地变为toValuekeyPath内容是CALayer的可动画Animatable属性
如果
fillMode=kCAFillModeForwards同时
removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
代码示例
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 创建动画对象 CABasicAnimation *anima = [CABasicAnimation animation]; // 设置修改的属性 // anima.keyPath = @"transform.scale"; anima.keyPath = @"position"; // 设置修改的值 anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)]; // 取消反弹 // 设置动画完成的时候不要移除 anima.removedOnCompletion = NO; // 动画保存最新的效果 anima.fillMode = kCAFillModeForwards; [_redView.layer addAnimation:anima forKey:nil]; }
CAKeyframeAnimation
CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值属性说明
values:里面的元素称为“关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
path:可以设置一个CGPathRef、CGMutablePathRef,让图层按照路径轨迹移动。path只对CALayer的anchorPoint和position起作用。如果设置了path,那么values将被忽略
keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧。如果没有设置keyTimes,各个关键帧的时间是平分的
代码示例
#define angle2radion(angle) ((angle) / 180.0 * M_PI ) - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 创建帧动画,多个值之间做动画 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // 设置修改的属性 anim.keyPath = @"position"; //设置动画时长 anim.duration = 1; // 来回抖动的动画 // anim.values = @[@(angle2radion(-5)),@(angle2radion(5)),@(angle2radion(-5))]; //根据path路径移动的动画 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 200)]; [path addLineToPoint:CGPointMake(250, 500)]; anim.path = path.CGPath; //动画的重复次数 anim.repeatCount = MAXFLOAT; [_imageView.layer addAnimation:anim forKey:nil]; }
CAAnimationGroup
是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行属性
animations:用来保存一组动画对象的NSArray
默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间
代码示例
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 创建一个动画组 CAAnimationGroup *group = [CAAnimationGroup animation]; // 平移 CAKeyframeAnimation *positionAnim = [CAKeyframeAnimation animation]; positionAnim.keyPath = @"position"; positionAnim.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 500)].CGPath; // 缩放 CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale"; anim.toValue = @0.1; // 旋转 CABasicAnimation *rotation = [CABasicAnimation animation]; rotation.keyPath = @"transform.rotation"; rotation.toValue = @(M_PI * 2); group.animations = @[positionAnim,rotation]; group.autoreverses = YES; group.repeatCount = MAXFLOAT; // 注意:通过设置动画组,操作动画时长 group.duration = 2; [_redView.layer addAnimation:group forKey:nil]; }
CATransition
CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点属性
type:动画过渡类型subtype:动画过渡方向
startProgress:动画起点(在整体动画的百分比)
endProgress:动画终点(在整体动画的百分比)
代码示例
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // UIView转场代码 // [UIView transitionWithView:_imageView duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{ // } completion:^(BOOL finished) { // }]; // 创建转场动画对象 CATransition *anim = [CATransition animation]; anim.duration = 2; // 设置转场类型 anim.type = @"pageCurl"; anim.startProgress = 0.5; anim.endProgress = 0.8; // 设置转场的方向, // anim.subtype = kCATransitionFromLeft; [_imageView.layer addAnimation:anim forKey:nil]; // 转场动画必须要和转场代码一起 }
转场动画的type
UIView动画函数实现转场动画
//单视图+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
//双视图
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion;
相关文章推荐
- MKNetworkKit 使用
- ACM暑期计划 —— 第三天
- 请珍惜身边的人
- 我这个站长是怎么炼成的
- Median of two Sorted Arrays
- 微软强制Win10家庭版用户自动安装所有系
- 经典排序算法分析及其Java实现
- CentOS安装Mysql5.5
- 课程设计——银行系统
- 初试zabbix基础概念
- 黑马程序员——基础学习感悟总结...
- 使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础
- 多线程程序设计学习(1)之多线程程序的评量标准
- 架构师速成8.2-架构师要懂产品
- 架构师速成8.2-架构师要懂产品 分类: 架构师速成 2015-07-18 07:25 232人阅读 评论(0) 收藏
- 架构师速成8.1-谈做技术人员的态度
- 架构师速成8.1-谈做技术人员的态度 分类: 架构师速成 2015-07-18 07:22 56人阅读 评论(0) 收藏
- python:任意输入3个数,判断能否组成三角形
- CentOS安装配置tengine并设置开机启动
- java 对象的生命周期