Core Animation 的入門門檻往往卡在兩個觀念:position 與 anchorPoint 的關係,以及 CABasicAnimation 那一長串屬性各自的意義。把這兩件事弄清楚,剩下的動畫需求幾乎都能自己組出來。
position:決定 CALayer 在父層中的位置,座標原點是父層的左上角 (0, 0)。
anchorPoint(錨點/定位點):決定 CALayer 上的**哪一個點**會對齊到 position 所指定的位置。它以自身左上角為原點,x 與 y 的範圍都是 0~1,預設值為 (0.5, 0.5),也就是中心點。
換句話說:position 是「放在哪裡」,anchorPoint 是「用身上的哪裡去對齊那個位置」。兩者相乘才決定 layer 的實際 frame。
這也解釋了一個常見困惑:當你改了 anchorPoint 卻沒有同步調整 position,整個控件看起來會「跳」到別的位置。因為 frame 的計算基準變了。正確做法是先記下舊錨點,改完後補償 position:
CGPoint oldAnchorPoint = _homeBtn.layer.anchorPoint;
_homeBtn.layer.anchorPoint = CGPointMake(0.5, 0);
[_homeBtn.layer setPosition:
CGPointMake(_homeBtn.layer.position.x
+ _homeBtn.layer.bounds.size.width * (_homeBtn.layer.anchorPoint.x - oldAnchorPoint.x),
_homeBtn.layer.position.y
+ _homeBtn.layer.bounds.size.height * (_homeBtn.layer.anchorPoint.y - oldAnchorPoint.y))];
把錨點移到頂邊,正是 tabbar 圖示「抖動/彈跳」動畫所需的設定——旋轉要以頂端為支點,而非中心。
建立 CABasicAnimation 時,用 fromValue 與 toValue 指定起點與終點,加入 layer 後即開始播放。
Autoreverses:設為 YES 時,動畫抵達終點後會**倒著播回起點**,而不是瞬間跳回。做「來回擺動」效果時必備。
Duration:從 fromValue 到 toValue 所花的時間(秒)。實際長度會受 speed 影響。
RemovedOnCompletion:預設 YES,動畫播完自動從 layer 移除,畫面回到 model layer 的狀態。若希望下次能重複使用同一個動畫物件,設為 NO。實務上要注意:設為 NO 時動畫的最終狀態不會保留,因為 CA 動畫預設不填滿(fillMode 為 removed),要保留終點狀態應改 model layer 的值或設定 fillMode = kCAFillModeForwards。
Speed:預設 1.0。設為 2.0 則播放速度加倍、時間減半。
BeginTime:在組動畫中指定相對的起始時刻,預設 0.0。
TimeOffset:讓動畫從中途某個點開始播放,常用於製造「已經進行到一半」的錯覺。
RepeatCount:預設 0(不重複)。要無限重複請使用 1e100f 或 HUGE_VALF。不可與 repeatDuration 併用。
RepeatDuration:以時間長度指定重複多久。同樣不可與 repeatCount 併用。
Cumulative:設為 YES 時,每一輪的數值會累加而非重頭開始,做連續旋轉時很重要。
CGPoint fromPoint = self.testImage.center;
// 以 controlPoint 為基準點建立曲線路徑
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];
CGPoint toPoint = CGPointMake(300, 460);
[movePath addQuadCurveToPoint:toPoint controlPoint:CGPointMake(300, 0)];
// 關鍵幀:設定動畫路徑
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(100, 100)],
[NSValue valueWithCGPoint:CGPointMake(100, 300)],
[NSValue valueWithCGPoint:CGPointMake(300, 300)],
[NSValue valueWithCGPoint:CGPointMake(300, 100)]];
moveAnimation.removedOnCompletion = YES;
// 縮放變化
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnimation.removedOnCompletion = YES;
// 旋轉
CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimation.fromValue = [NSNumber numberWithFloat:0.f];
rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI];
rotateAnimation.cumulative = YES;
rotateAnimation.removedOnCompletion = YES;
// 組動畫
CAAnimationGroup *animaGroup = [CAAnimationGroup animation];
animaGroup.animations = @[moveAnimation, scaleAnimation, rotateAnimation];
animaGroup.duration = 2.f;
透明度變化也可以加入:
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; opacityAnimation.fromValue = @1.0; opacityAnimation.toValue = @0.1;
透過替換 animationWithKeyPath: 的參數即可改變動畫對象:
· transform.scale / .x / .y —— 比例、寬、高
· transform.rotation.z —— 平面旋轉
· opacity —— 透明度
· backgroundColor —— 背景色
· cornerRadius —— 圓角
· borderWidth —— 邊框寬度
· bounds / frame / position —— 尺寸與位置
· contents / contentsRect —— 內容圖與顯示區域
· hidden / masksToBounds / mask —— 可見性與遮罩
· shadowColor / shadowOffset / shadowOpacity / shadowRadius —— 陰影
· zPosition / margin
注意 transform 與 frame 不能同時動畫——frame 是由 position、bounds、anchorPoint 一起算出來的,直接改 frame 會與 transform 互相干擾。
類似 58 同城那種點擊後圖示左右搖晃的效果,用 CABasicAnimation 就能做到:
CGPoint oldAnchorPoint = _homeBtn.layer.anchorPoint;
_homeBtn.layer.anchorPoint = CGPointMake(0.5, 0);
[_homeBtn.layer setPosition:
CGPointMake(_homeBtn.layer.position.x
+ _homeBtn.layer.bounds.size.width * (_homeBtn.layer.anchorPoint.x - oldAnchorPoint.x),
_homeBtn.layer.position.y
+ _homeBtn.layer.bounds.size.height * (_homeBtn.layer.anchorPoint.y - oldAnchorPoint.y))];
CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
shakeAnimation.duration = 0.07;
shakeAnimation.autoreverses = YES; // 到達終點後倒著播回,形成來回擺動
shakeAnimation.repeatCount = 2;
shakeAnimation.removedOnCompletion = NO;
shakeAnimation.fromValue = [NSNumber numberWithFloat:-0.05];
shakeAnimation.toValue = [NSNumber numberWithFloat:0.05];
[self.homeBtn.layer addAnimation:shakeAnimation forKey:@"shakeAnimation"];
拆解一下為什麼有效:把錨點移到頂端,旋轉支點就落在圖示上方;autoreverses 讓它 −0.05 到 0.05 之間來回;repeatCount = 2 加上自動倒放,實際擺動次數是 4 次;總時長 0.07 × 4 ≈ 0.28 秒,短促而有節奏。
1. 動畫不會改變 model 值。CABasicAnimation 只影響畫面呈現(presentation layer),動畫結束後 layer 會回到 model layer 的狀態。若希望最終停在終點,請同步更新 model 屬性。
2. 需要監聽完成事件時用 CAAnimationDelegate。實作 animationDidStop:finished:,並注意 finished 在動畫被中斷時會是 NO。
3. 無法動畫的屬性。hidden、contents(圖片切換)、部分文字屬性並不能平順插值,需要動畫時請改用透明度或遮罩變化。
4. 大量元素建議改用 UIViewPropertyAnimator 或系統的 animateKeyframes。若只是簡單的位移、縮放、透明度變化,UIView animateWithDuration: 的程式碼量遠少於 Core Animation,且自動處理 completion;只有需要 keyPath、CA 特有的 fillMode、時間函式微調時,才值得直接用 CABasicAnimation。