CAAnimation
是一個抽象類,遵循了CAMediaTiming
協議和CAAction
協議!咱們不要直接使用CAAnimation
類,而是使用其子類:app
CATransition
:提供漸變效果,如推拉push
效果,消退fade
效果,揭開reveal
效果CAAnimationGroup
:容許多個動畫同時播放CABasicAnimation
: 提供了對單一動畫的實現CAKeyframeAnimation
: 關鍵楨動畫,能夠定義動畫路線CAPropertyAnimation
:屬性動畫,一般不直接使用,而是使用CABasicAnimation
子類咱們看到有一個工廠方法來建立CAAnimation
對象,所以,咱們一般都使用這個方法來建立動畫:ide
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;
|
這個協議是是用於配置動畫的相關屬性的,英文部分是官方的註釋,中文部分爲筆者的理解,下面一一講解:ui
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;
|
這個協議只有一個方法,咱們能夠調用此方法來觸發指定的事件,這樣接收者就能夠接收到代理。this
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;
|
CAAnimation
爲這麼個屬性:spa
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;
|
當咱們動畫完成時,若是但願動畫就自動移除的話,咱們能夠設置此屬性爲YES
,默認值爲YES
。若是咱們想要循環或者執行屢次動畫,就將此屬性設置爲NO
code
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;
|
這個屬性是用於指定動畫移動的步調是什麼樣式,好比線性。對象
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;
|