序言

CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类:

  • CATransition:提供渐变效果,如推拉push效果,消退fade效果,揭开reveal效果
  • CAAnimationGroup:允许多个动画同时播放
  • CABasicAnimation: 提供了对单一动画的实现
  • CAKeyframeAnimation: 关键桢动画,可以定义动画路线
  • CAPropertyAnimation:属性动画,通常不直接使用,而是使用CABasicAnimation子类

创建对象

我们看到有一个工厂方法来创建CAAnimation对象,因此,我们通常都使用这个方法来创建动画:

 
1
2
3
 
+ (instancetype)animation;
 

当然不同类型的子类使用的方法不一样,对于继承于CAPropertyAnimation的子类,都可以通过属性路径来创建:

 
1
2
3
4
5
6
 
/* Creates a new animation object with its `keyPath' property set to
* 'path'. */
 
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
 

遵守了CAMediaTiming协议

这个协议是是用于配置动画的相关属性的,英文部分是官方的注释,中文部分为笔者的理解,下面一一讲解:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
/* The begin time of the object, in relation to its parent object, if
* applicable. Defaults to 0. */
// 获取或设置动画的开始时间,默认为0
@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. */
// 获取或设置动画的播放速度,默认为1,若设置为2,则以两倍的速度播放。
// 如果设置小于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. */
// 获取或设置当前播放的进度,默认为0。有这么一种使用场景:设置speed为0,
// 然后设置这个timeOffset为合适的值,就可以暂停动画了
@property CFTimeInterval timeOffset;
 
/* The repeat count of the object. May be fractional. Defaults to 0. */
// 获取或设置动画播放次数,默认为0表示只播放一次。
// 设置为HUGE_VALF表示无限制播放次数
@property float repeatCount;
 
/* The repeat duration of the object. Defaults to 0. */
// 获取或设置重复播放的动画时长,不要与repeatCount混合使用
@property CFTimeInterval repeatDuration;
 
/* When true, the object plays backwards after playing forwards. Defaults
* to NO. */
// 获取或设置是否回放。
// 如果设置为YES,在动画播放完成时,就会以动画的效果回到起点
// 如果设置为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'. */
// 获取或设置动画完成时的动作
// forwards表示动画完成时,也回到起点而不是留在终点
// backwards表示动画完成时,就停留在终点
// removed表示完成时就移除,默认就是removed
@property(copy) NSString *fillMode;
 

遵守了CAAction协议

这个协议只有一个方法,我们可以调用此方法来触发指定的事件,这样接收者就可以接收到代理。

 
1
2
3
4
5
6
7
8
9
10
 
/* Called to trigger the event named 'path' on the receiver. The object
* (e.g. the layer) on which the event happened is 'anObject'. The
* arguments dictionary may be nil, if non-nil it carries parameters
* associated with the event. */
 
- (void)runActionForKey:(NSString *)event
                 object:(id)anObject
              arguments:(nullable NSDictionary *)dict;
 

CAAnimationDelegate代理

CAAnimation为这么个属性:

 
1
2
3
4
5
6
7
 
/* The delegate of the animation. This object is retained for the
* lifetime of the animation object. Defaults to nil. See below for the
* supported delegate methods. */
 
@property(nullable, strong) id delegate;
 

我们只要指定了代理,就可以实现这两个代理方法:

 
1
2
3
4
5
6
7
8
9
10
11
12
 
/* Called when the animation begins its active duration. */
// 动画开始时的回调
- (void)animationDidStart:(CAAnimation *)anim;
 
/* Called when the animation either completes its active duration or
* is removed from the object it is attached to (i.e. the layer). 'flag'
* is true if the animation reached the end of its active duration
* without being removed. */
// 动画停止的回调,可以通过flag判断动画是否是完成还是暂停
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
 

removedOnCompletion属性

当我们动画完成时,如果希望动画就自动移除的话,我们可以设置此属性为YES,默认值为YES。如果我们想要循环或者执行多次动画,就将此属性设置为NO

 
1
2
3
4
5
6
7
 
/* When true, the animation is removed from the render tree once its
* active duration has passed. Defaults to YES. */
 
// 当duration值已经达到时,是否将动画自动从渲染树上移除
@property(getter=isRemovedOnCompletion) BOOL removedOnCompletion;
 

timingFunction属性

这个属性是用于指定动画移动的步调是什么样式,比如线性。

 
1
2
3
4
5
6
 
/* A timing function defining the pacing of the animation. Defaults to
* nil indicating linear pacing. */
 
@property(nullable, strong) CAMediaTimingFunction *timingFunction;
 

关于CAMediaTimingFunction类,主要是这向个方法。当创建时,我们+functionWithName:工厂方法来创建系统已经提供的样式。

 
1
2
3
4
5
6
7
8
 
/* A convenience method for creating common timing functions. The
* currently supported names are `linear', `easeIn', `easeOut' and
* `easeInEaseOut' and `default' (the curve used by implicit animations
* created by Core Animation). */
 
+ (instancetype)functionWithName:(NSString *)name;
 

其中这个name有这几个变量对应的:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
// 线性动画
CA_EXTERN NSString * const kCAMediaTimingFunctionLinear
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 
// 快速进入动画
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 
// 快速出来动画
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 
// 快速进入出来动画
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
    
// 默认动画是curve动画,也就是曲线动画
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault
    __OSX_AVAILABLE_STARTING (__MAC_10_6, __IPHONE_3_0);
 

如果我们想要让其移动动画是按贝塞尔曲线的路径行动,那么可以用这两个方法来创建:

 
1
2
3
4
5
6
7
8
9
10
 
/* Creates a timing function modelled on a cubic Bezier curve. The end
* points of the curve are at (0,0) and (1,1), the two points 'c1' and
* 'c2' defined by the class instance are the control points. Thus the
* points defining the Bezier curve are: '[(0,0), c1, c2, (1,1)]' */
 
+ (instancetype)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
 
- (instancetype)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;

CAAnimation的更多相关文章

  1. 再谈CAAnimation动画

    CAAnimaton动画分为CABasicAnimation & CAKeyframeAnimation CABasicAnimation动画, 顾名思义就是最基本的动画, 老规矩先上代码: ...

  2. Quartz2D复习(四) --- 图层CALayer和动画CAAnimation

    1.CALayer 1).在ios中,能看得见摸得着的东西基本上都是UIView, 比如按钮.文本标签.文本输入框.图标等,这些都是UIView 2).UIView之所以能显示在屏幕上,完全是因为它内 ...

  3. iOS:核心动画的详解介绍:CAAnimation(抽象类)及其子类

    核心动画的详解介绍:CAAnimation(抽象类)   1.核心动画基本概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍! 使用它 ...

  4. CAAnimation解读

    序言 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果 ...

  5. - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法浅析

    转载自:http://blog.csdn.net/ronaldo_carry/article/details/49070119 将viewdidload里面的代码全部注释掉 - (void)viewD ...

  6. 核心动画 CAAnimation 进阶

    转载自:http://www.cofcool.net/development/2015/06/20/ios-study-note-nine-CoreAnimation/ Core Animation, ...

  7. iOS开发CAAnimation详解

    Core Animation,即为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能.Core Anima ...

  8. CAAnimation 动画支撑系统

    Model支撑:(依附对象) 从presentLayer获取数据: 渲染树为私有: -(void)addAnimation:(CAAnimation *)anim forKey:(NSString * ...

  9. iOS开发CAAnimation类动画, CATransition动画

    #pragma mark - CAAnimation类.实现动画 #pragma mark ** CABasicAnimation 动画 - (IBAction)handleCABasicAnimat ...

  10. 阶段性总结⓵触摸事件&手势识别⓶Quartz2D绘图⓷CALayer图层⓸CAAnimation⓹UIDynamic UI动力学⓺KVC&KVO

    知识点复习   1. 触摸事件&手势识别   1> 4个触摸事件,针对视图的 2> 6个手势识别(除了用代码添加,也可以用Storyboard添加)   附加在某一个特定视图上的, ...

随机推荐

  1. Python变量及数据类型

    所有编程语言几乎都会有 ’ 变量‘ ,如 a = 2,用一个a变量指代数字2,在Python中,一切皆对象,因此在变量赋值的时候实际上是在内存中开辟了一块存储变量内容的内存空间对象. 对象可以指定不同 ...

  2. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  3. 机器学习基础-Logistic回归2

    随机梯度上升法--一次仅用一个样本点来更新回归系数(因为可以在新样本到来时对分类器进行增量式更新,因而属于在线学习算法) 梯度上升法在每次更新回归系统时都需要遍历整个数据集,该方法在处理100个左右的 ...

  4. HDU1686 计算模式串匹配的次数

    题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于1000000,w长度小于10000,计算W匹配到T中成功的次数: 这题很明显要用KMP算法,不然很容易 ...

  5. 【组合数模板】HDU 6114 Chess

    http://acm.hdu.edu.cn/showproblem.php?pid=6114 [思路] 就是求C(m,n) [板] #include<iostream> #include& ...

  6. 任务查询系统(bzoj 3932)

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...

  7. SGU 104 Little shop of flowers【DP】

    浪(吃)了一天,水道题冷静冷静.... 题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=104 题意: 给定每朵花放在每个花盆的值, ...

  8. HashCode和equal方法

    equals()反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用,也可能是值类型的值. 而hashCode()是对象或变量通过哈希算法计算出的哈希值. 之所以有hashCode方 ...

  9. 在SUSE12中使用 Machinery 进行高级系统管理

    简单介绍 在 SUSE Linux Enterprise 12 中.SUSE 如今推出了面向系统管理员的 Machinery.作为其高级系统管理模块的一部分.Machinery 是适用于 Linux ...

  10. POJ 1436 Horizontally Visible Segments(线段树)

    POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...