序言

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. iOS开发CoreAnimation解读之二——对CALayer的分析

    iOS开发CoreAnimation解读之二——对CALayer的分析 一.UIView中的CALayer属性 1.Layer专门负责view的视图渲染 2.自定义view默认layer属性的类 二. ...

  2. SDWebImage源码解读之SDWebImageDownloaderOperation

    第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...

  3. 再谈CAAnimation动画

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

  4. SDWebImage源码解读 之 NSData+ImageContentType

    第一篇 前言 从今天开始,我将开启一段源码解读的旅途了.在这里先暂时不透露具体解读的源码到底是哪些?因为也可能随着解读的进行会更改计划.但能够肯定的是,这一系列之中肯定会有Swift版本的代码. 说说 ...

  5. SDWebImage源码解读 之 UIImage+GIF

    第二篇 前言 本篇是和GIF相关的一个UIImage的分类.主要提供了三个方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name ----- 根据名 ...

  6. SDWebImage源码解读 之 SDWebImageCompat

    第三篇 前言 本篇主要解读SDWebImage的配置文件.正如compat的定义,该配置文件主要是兼容Apple的其他设备.也许我们真实的开发平台只有一个,但考虑各个平台的兼容性,对于框架有着很重要的 ...

  7. SDWebImage源码解读_之SDWebImageDecoder

    第四篇 前言 首先,我们要弄明白一个问题? 为什么要对UIImage进行解码呢?难道不能直接使用吗? 其实不解码也是可以使用的,假如说我们通过imageNamed:来加载image,系统默认会在主线程 ...

  8. SDWebImage源码解读之SDWebImageCache(上)

    第五篇 前言 本篇主要讲解图片缓存类的知识,虽然只涉及了图片方面的缓存的设计,但思想同样适用于别的方面的设计.在架构上来说,缓存算是存储设计的一部分.我们把各种不同的存储内容按照功能进行切割后,图片缓 ...

  9. SDWebImage源码解读之SDWebImageCache(下)

    第六篇 前言 我们在SDWebImageCache(上)中了解了这个缓存类大概的功能是什么?那么接下来就要看看这些功能是如何实现的? 再次强调,不管是图片的缓存还是其他各种不同形式的缓存,在原理上都极 ...

随机推荐

  1. 关于BigDecimal的四舍五入和截断 (2007-08-10 15:06:26)

    关于四舍五入:ROUND_HALF_UP: 遇到.5的情况时往上近似,例: 1.5 ->;2ROUND_HALF_DOWN : 遇到.5的情况时往下近似,例: 1.5 ->;1 BigDe ...

  2. uva 111 - History Grading (dp, LCS)

    题目链接 题意:给N,第二行是答案,n个数c1---cn, 代表第一个的顺序是c1,第二个数顺序是c2; 下面每一行是学生的答案,格式同上. 注意:这个给的顺序需要处理一下,不能直接用. 思路:LCS ...

  3. UVa 1400 (线段树) "Ray, Pass me the dishes!"

    求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...

  4. UITableViewCell上的按钮点击事件处理

    转自:  http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...

  5. Asp.Net IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  6. [swustoj 771] 奶牛农场

    奶牛农场 Description 将军有一个用栅栏围成的矩形农场和一只奶牛,在农场的一个角落放有一只矩形的箱子,有一天将军要出门,他就把奶牛用一根绳子套牢,然后将绳子的另一端绑到了那个箱子不靠栅栏的角 ...

  7. 移动APP服务端API设计应该考虑到的问题

    2014年,移动APP的热度丝毫没有减退,并没有像桌面软件被WEB网站那样所取代, 不但如此,越来越多的传统应用.网站也都开始制作自己的移动APP,也就是我们常说的IOS客户端.android客户端. ...

  8. T-SQL备忘(1):表联接

    测试用例表如下: 1.取2个成员表中的交集(A∩B) T-SQL: select Member1.Name,Member1.Age from Member1 join Member2 on Membe ...

  9. 【jQuery】鼠标接触按钮后改变图片

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  10. ubuntu-12.04.1-desktop-x64下JDK环境的安装与配置

    1.上oracle官网下载最新的JDK.在这里,我的系统是ubuntu-12.04.1-desktop-amd64,目前位置JDK的最新版本位7u9.jdk-for-linux有两种安装包,一种是rp ...