核心動畫
標籤(空格分隔): 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或者MAXFLOATrepeatDuration
:重複時間removedOnCompletion
:默認爲YES,代表動畫執行完畢後就從圖層上移除,圖形會恢復到動畫執行前的狀態。如果想讓圖層保持顯示動畫執行後的狀態,那就設置爲NO,不過還要設置fillMode爲kCAFillModeForwardsfillMode
:決定當前對象在非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;