Core Animation 是 iOS 動畫的底層引擎,幾乎所有流暢的動態效果都建立在它之上。本文依序介紹基礎動畫、關鍵幀動畫、轉場動畫,以及 UIView 封裝的動畫接口。
基礎動畫用於「從一個狀態過渡到另一個狀態」,透過 keyPath 指定要動的屬性。
// 1. 初始化 CABasicAnimation *cabase = [CABasicAnimation animation]; // 2. 透過 keyPath 指定動畫作用的屬性,此處為 bounds cabase.keyPath = @"bounds"; // 3. 設定狀態值 cabase.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 10, 100)]; // 結束狀態 cabase.byValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 10, 100)]; // 每次改變的範圍 cabase.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; // 起始狀態 // 4. 設定時長與保留策略 cabase.duration = 2; cabase.fillMode = kCAFillModeForwards; // 動畫結束後保留最後狀態 cabase.removedOnCompletion = NO; // 不從 layer 上移除 [_layer addAnimation:cabase forKey:nil];
案例:用基礎動畫做仿射縮放
CABasicAnimation *cabase = [CABasicAnimation animation]; cabase.keyPath = @"transform"; cabase.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 2, 1)]; cabase.duration = 2; cabase.fillMode = kCAFillModeForwards; cabase.removedOnCompletion = NO; [_layer addAnimation:cabase forKey:nil];
當路徑不是直線,需要經過多個中間點時,就用關鍵幀動畫。
// 1. 初始化 CAKeyframeAnimation *keyfram = [CAKeyframeAnimation animation]; // 2. 指定屬性,此處為 position keyfram.keyPath = @"position"; // 3. 設定動畫需要經過的一系列點 CGPoint p1 = CGPointZero; CGPoint p2 = CGPointMake(150, 0); CGPoint p3 = CGPointMake(150, 150); CGPoint p4 = CGPointMake(0, 150); CGPoint p5 = CGPointZero; NSValue *v1 = [NSValue valueWithCGPoint:p1]; NSValue *v2 = [NSValue valueWithCGPoint:p2]; NSValue *v3 = [NSValue valueWithCGPoint:p3]; NSValue *v4 = [NSValue valueWithCGPoint:p4]; NSValue *v5 = [NSValue valueWithCGPoint:p5]; // 4. 套用數值並保留動畫 keyfram.values = @[v1, v2, v3, v4, v5]; keyfram.duration = 1; keyfram.fillMode = kCAFillModeForwards; keyfram.removedOnCompletion = NO; [_layer addAnimation:keyfram forKey:nil];
案例:讓圖片左右搖擺
CAKeyframeAnimation *anima = [CAKeyframeAnimation animation]; // 透過旋轉角度的關鍵幀來實現搖擺 anima.keyPath = @"transform.rotation"; float p1 = 4 / 180.0 * M_PI; // 4 度轉為弧度 anima.duration = 0.2; anima.values = @[@(-p1), @(p1), @(-p1)]; anima.fillMode = kCAFillModeForwards; anima.removedOnCompletion = NO; anima.repeatCount = MAXFLOAT; // 無限次重複 [_layer addAnimation:anima forKey:nil]; _layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 0);
轉場動畫通常用於整體畫面的切換。
// 1. 初始化 CATransition *tran = [CATransition animation]; // 2. 設定動畫效果,例如 rippleEffect // 常用效果:kCATransitionFade、kCATransitionMoveIn、kCATransitionPush、kCATransitionReveal tran.type = @"rippleEffect"; // 3. 設定動畫方向 // kCATransitionFromRight、kCATransitionFromLeft、kCATransitionFromTop、kCATransitionFromBottom tran.subtype = kCATransitionFromLeft; // 4. 設定保留策略與時長 tran.fillMode = kCAFillModeForwards; tran.removedOnCompletion = NO; tran.duration = 1; [self.myImageView.layer addAnimation:tran forKey:nil];
UIKit 把動畫直接整合進 UIView:當內部某些屬性改變時,只要把改變的程式碼包在 beginAnimations:context: 與 commitAnimations 之間,系統就會自動補上動畫。
常用方法:
// 設定動畫代理
+ (void)setAnimationDelegate:(id)delegate
// 動畫即將開始時呼叫的 selector
+ (void)setAnimationWillStartSelector:(SEL)selector
// 動畫結束時呼叫的 selector
+ (void)setAnimationDidStopSelector:(SEL)selector
// 動畫持續時間
+ (void)setAnimationDuration:(NSTimeInterval)duration
// 動畫延遲
+ (void)setAnimationDelay:(NSTimeInterval)delay
// 動畫開始時間
+ (void)setAnimationStartDate:(NSDate *)startDate
// 動畫節奏(緩動曲線)
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve
// 重複次數
+ (void)setAnimationRepeatCount:(float)repeatCount
// 設為 YES 時,每次重複的效果會與上一次相反
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
// 設定 view 的轉場效果,cache 設為 YES 可複用快取、效能較佳
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition
forView:(UIView *)view
cache:(BOOL)cache
案例一:旋轉動畫
[UIView beginAnimations:@"roate" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView setAnimationDuration:1.5]; [UIView setAnimationDelegate:self]; _view.transform = CGAffineTransformRotate(_view.transform, M_PI_2); [UIView setAnimationDidStopSelector:@selector(endAnimate)]; [UIView commitAnimations];
案例二:轉場動畫
[UIView beginAnimations:@"transition" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:_mainView
cache:YES];
[UIView setAnimationDuration:1.5];
NSInteger index1 = [_mainView.subviews indexOfObject:_view];
NSInteger index2 = [_mainView.subviews indexOfObject:_view2];
[_mainView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
[UIView commitAnimations];
選型建議:簡單的屬性變化用 CABasicAnimation 或 UIView 動畫即可;路徑複雜用 CAKeyframeAnimation;整個畫面切換用 CATransition。若追求效能與可控性,記得動畫應盡量作用在 layer 上,避免觸發不必要的 layout 與重繪。