Core Animation
2016-05-12 10:05
369 查看
Core Animation,中文翻译是核心动画,他是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往是事半功倍。也就是说,使用少量的代码就可以实现非常强大的功能。
Core Animation可以用在MacOS X 和iOS平台
Core Animation的动画执行过程都是在后台操作的,不会租塞主线程
Core Animation是直接作用在CALayer上的,并非UIView
核心动画的框架如下:
图一
蓝色代表继承某个类 红色代表遵守某个协议
图二
图一和图二表达意思类似,图二中虚线框中的类为虚类,实线框为实例,绿色框未协议,核心动画中的虚类是不能使用的,使用其子类。
初始化一个CAAnimation对象,并设置一些动画相关属性
通过调用CALayer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始执行动画了
通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画
CAAnimation中的一些属性:
removedOnCompletion:默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为– kCAFillModeForwards
fillMode决定当前对象在非active时间段的行为。(要想fillMode有效,最好设置removedOnCompletion = NO)
。kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
。kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
。kCAFillModeBackwards 在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
。kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
kCAMediaTimingFunctionLinear(线性):匀速,给你一个相对静态的感觉
kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
CAAnimation代理方法
CAAnimation在分类中定义了代理方法。是给NSObject添加的分类,所以任何对象,成为CAAnimation的代理,都可以。
动画开始的时候调用
动画停止的时候调用
CALayer上动画的暂停和恢复
暂停
恢复
属性及其说明
动画过程:
* 随着动画的进行,在长度为duration的持续时间内,keyPath相应属性的值从fromValue渐渐地变为toValue
* keyPath内容是CALayer的可动画Animatable属性
* 如果fillMode=kCAFillModeForwards同时removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
示例:
CABasicAnimation只能从一个数值(FromValue)变到另一个数值(toValue),而CAKeyFrameAnimation会使用一个NSArray保存这些数值。
CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation
属性说明:
* values:上述的NSArray对象。里面的元素称为“关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
* path:可以设置一个CGPathRef、CGMutablePathRef,让图层按照路径轨迹移动。path只对CALayer的anchorPoint和position起作用。如果设置了path,那么values将被忽略
* keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧。如果没有设置keyTimes,各个关键帧的时间是平分
示例:
属性说明:
* animations:用来保存一组动画对象的NSArray
示例:
UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果
动画属性:
type:动画过渡类型
subtype:动画过渡方向
startProgress:动画起点(在整体动画的百分比)
endProgress:动画终点(在整体动画的百分比)
fade : 交叉淡化过渡
push : 新视图把旧视图推出去
moveIn: 新视图移到旧视图上面
reveal: 将旧视图移开,显示下面的新视图
cube : 立方体翻滚效果
oglFlip : 上下左右翻转效果
suckEffect : 收缩效果,如一块布被抽走
rippleEffect: 水滴效果
pageCurl : 向上翻页效果
pageUnCurl : 向下翻页效果
cameraIrisHollowOpen : 相机镜头打开效果
cameraIrisHollowClose : 相机镜头关闭效果
fromRight
fromTop
fromBottom
示例:
Core Animation可以用在MacOS X 和iOS平台
Core Animation的动画执行过程都是在后台操作的,不会租塞主线程
Core Animation是直接作用在CALayer上的,并非UIView
核心动画的框架如下:
图一
蓝色代表继承某个类 红色代表遵守某个协议
图二
图一和图二表达意思类似,图二中虚线框中的类为虚类,实线框为实例,绿色框未协议,核心动画中的虚类是不能使用的,使用其子类。
使用步骤
首先得有CALayer(因为CoreAnimation是作用在CALayer上的)初始化一个CAAnimation对象,并设置一些动画相关属性
通过调用CALayer的addAnimation:forKey:方法,增加CAAnimation对象到CALayer中,这样就能开始执行动画了
通过调用CALayer的removeAnimationForKey:方法可以停止CALayer中的动画
CAAnimation中的一些属性:
* The begin time of the object, in relation to its parent object, if * applicable. Defaults to 0. */ * 可以用来设置动画延迟执行时间,若想延迟2s,就设置为CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间 @property CFTimeInterval beginTime; /* The basic duration of the object. Defaults to 0. */ @property CFTimeInterval duration; /* The rate of the layer. Used to scale parent time to local time, e.g. * if rate is 2, local time progresses twice as fast as parent time. * Defaults to 1. */ @property float speed; /* Additional offset in active local time. i.e. to convert from parent * time tp to active local time t: t = (tp - begin) * speed + offset. * One use of this is to "pause" a layer by setting `speed' to zero and * `offset' to a suitable value. Defaults to 0. */ @property CFTimeInterval timeOffset; /* The repeat count of the object. May be fractional. Defaults to 0. */ @property float repeatCount; /* The repeat duration of the object. Defaults to 0. */ @property CFTimeInterval repeatDuration; /* When true, the object plays backwards after playing forwards. Defaults * to NO. */ @property BOOL autoreverses; /* Defines how the timed object behaves outside its active duration. * Local time may be clamped to either end of the active duration, or * the element may be removed from the presentation. The legal values * are `backwards', `forwards', `both' and `removed'. Defaults to * `removed'. */ @property(copy) NSString *fillMode;
removedOnCompletion:默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为– kCAFillModeForwards
fillMode决定当前对象在非active时间段的行为。(要想fillMode有效,最好设置removedOnCompletion = NO)
。kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
。kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
。kCAFillModeBackwards 在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
。kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
kCAMediaTimingFunctionLinear(线性):匀速,给你一个相对静态的感觉
kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地
CAAnimation代理方法
CAAnimation在分类中定义了代理方法。是给NSObject添加的分类,所以任何对象,成为CAAnimation的代理,都可以。
动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim;
动画停止的时候调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
CALayer上动画的暂停和恢复
暂停
-(void)pauseLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; // 让CALayer的时间停止走动 layer.speed = 0.0; // 让CALayer的时间停留在pausedTime这个时刻 layer.timeOffset = pausedTime; }
恢复
-(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; }
CABasicAnimation
基本动画,是CAPropertyAnimation的子类/** Subclass for basic (single-keyframe) animations. **/ @interface CABasicAnimation : CAPropertyAnimation
属性及其说明
/* The objects defining the property values being interpolated between. * All are optional, and no more than two should be non-nil. The object * type should match the type of the property being animated (using the * standard rules described in CALayer.h). The supported modes of * animation are: * * - both `fromValue' and `toValue' non-nil. Interpolates between * `fromValue' and `toValue'. * * - `fromValue' and `byValue' non-nil. Interpolates between * `fromValue' and `fromValue' plus `byValue'. * * - `byValue' and `toValue' non-nil. Interpolates between `toValue' * minus `byValue' and `toValue'. * * - `fromValue' non-nil. Interpolates between `fromValue' and the * current presentation value of the property. * * - `toValue' non-nil. Interpolates between the layer's current value * of the property in the render tree and `toValue'. * * - `byValue' non-nil. Interpolates between the layer's current value * of the property in the render tree and that plus `byValue'. */ @property(nullable, strong) id fromValue; @property(nullable, strong) id toValue; @property(nullable, strong) id byValue;
动画过程:
* 随着动画的进行,在长度为duration的持续时间内,keyPath相应属性的值从fromValue渐渐地变为toValue
* keyPath内容是CALayer的可动画Animatable属性
* 如果fillMode=kCAFillModeForwards同时removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。
示例:
CABasicAnimation *anim = [CABasicAnimation animation]; anim.duration = 1.0; // 移动 // anim.keyPath = @"position"; // anim.toValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 缩放 // 只能是layer的属性 anim.keyPath = @"transform.scale"; anim.toValue = @0.5; anim.repeatCount = MAXFLOAT; anim.removedOnCompletion = NO; anim.fillMode = kCAFillModeForwards; [_tripView.layer addAnimation:anim forKey:nil];
CAKeyframeAnimation 关键帧动画
它也是CAPropertyAnimation的子类,与CABasicAnimation的区别是:CABasicAnimation只能从一个数值(FromValue)变到另一个数值(toValue),而CAKeyFrameAnimation会使用一个NSArray保存这些数值。
CABasicAnimation可看做是只有2个关键帧的CAKeyframeAnimation
属性说明:
* values:上述的NSArray对象。里面的元素称为“关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
* path:可以设置一个CGPathRef、CGMutablePathRef,让图层按照路径轨迹移动。path只对CALayer的anchorPoint和position起作用。如果设置了path,那么values将被忽略
* keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧。如果没有设置keyTimes,各个关键帧的时间是平分
示例:
// 核心动画 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // anim.keyPath = @"transform.rotation"; // anim.values = @[@(- M_PI_4 / 5), @(M_PI_4 / 5), @(- M_PI_4 / 5)]; anim.keyPath = @"position"; anim.path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 200, 500)].CGPath; anim.duration = 2.0; anim.repeatCount = MAXFLOAT; [_tripView.layer addAnimation:anim forKey:nil];
CAAnimationGroup——动画组
动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行,默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间属性说明:
* animations:用来保存一组动画对象的NSArray
示例:
// 动画组 CAAnimationGroup *group = [CAAnimationGroup animation]; CABasicAnimation *scale = [CABasicAnimation animation]; scale.keyPath = @"transform.scale"; scale.toValue = @0.5; CABasicAnimation *rotation = [CABasicAnimation animation]; rotation.keyPath = @"transform.rotation"; rotation.toValue = @(M_PI_4); group.animations = @[scale, rotation]; [_showI.layer addAnimation:group forKey:nil];
CATransition 转场动画
CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果
动画属性:
type:动画过渡类型
subtype:动画过渡方向
startProgress:动画起点(在整体动画的百分比)
endProgress:动画终点(在整体动画的百分比)
type特性
转场动画的类型(NSString *type)fade : 交叉淡化过渡
push : 新视图把旧视图推出去
moveIn: 新视图移到旧视图上面
reveal: 将旧视图移开,显示下面的新视图
cube : 立方体翻滚效果
oglFlip : 上下左右翻转效果
suckEffect : 收缩效果,如一块布被抽走
rippleEffect: 水滴效果
pageCurl : 向上翻页效果
pageUnCurl : 向下翻页效果
cameraIrisHollowOpen : 相机镜头打开效果
cameraIrisHollowClose : 相机镜头关闭效果
subtype
fromLeftfromRight
fromTop
fromBottom
示例:
static int i = 2; NSString *imageName = [NSString stringWithFormat:@"%d", i]; _showI.image = [UIImage imageNamed:imageName]; if (i >= 3 || i < 2) { i = 2; } else { i++; } //转场动画 CATransition *anim = [CATransition animation]; anim.duration = 4.0; anim.subtype = @"fromRight"; anim.type = @"cube"; anim.startProgress = 0.5; anim.endProgress = 0.8; anim.repeatCount = MAXFLOAT; [_showI.layer addAnimation:anim forKey:nil];
相关文章推荐
- 21分钟mysql入门教程
- JS实现表单的验证
- map几种方法
- 公众号判断用户是否已关注php代码
- OC 对象和匿名对象
- OpenWRT with OpenvSwitch 刷路由器教程
- Mysql迁移到Oracle简单记录
- 分享,请不要忽视了作者的版权
- 输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印
- Android 开发判断用户是否安装微信和QQ并跳转过去
- 浅谈java中的堆栈
- 创业的第一百八十八天
- Runner之记计帐项目的典型用户和用户场景
- std::pair<MAP::iterator,bool>用法,可用来判断插入式否成功
- JS省市联动代码
- 玩家心理
- AFNetworking 3.0
- 我眼中的KMP
- 机器学习 周志华 读书笔记 习题1.2
- ABAP开发中的SMARTFORMS 参数