iOS 中的動畫主要分兩大類:UIView 的視圖動畫,以及 Layer 的動畫。UIView 的動畫其實也是建立在 Layer 動畫之上的簡化介面。兩者的程式碼格式都相當固定,熟悉之後基本可以套用模板。
[UIView beginAnimations:@"ddd" context:nil]; // 開始動畫區塊
[UIView commitAnimations]; // 提交動畫這兩句是必須成對出現的,所有動畫程式碼都要寫在中間。
[UIView beginAnimations:@"ddd" context:nil]; // ddd 為動畫名稱
[UIView setAnimationDuration:3]; // 動畫持續時間
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 加減速方式
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.window cache:YES]; // 轉場樣式,forView 指定作用對象
[UIView setAnimationDelay:3]; // 延遲多久才開始
[UIView setAnimationDelegate:self]; // 動畫代理,須在 commit 之前設定
[UIView setAnimationWillStartSelector:@selector(star)]; // 即將開始時呼叫
[UIView setAnimationDidStopSelector:@selector(stop)]; // 結束後呼叫
[UIView commitAnimations]; // 提交動畫轉場樣式的可選值:
typedef enum {
UIViewAnimationTransitionNone, // 普通狀態
UIViewAnimationTransitionFlipFromLeft, // 從左往右翻轉
UIViewAnimationTransitionFlipFromRight, // 從右往左翻轉
UIViewAnimationTransitionCurlUp, // 向上翻頁
UIViewAnimationTransitionCurlDown, // 向下翻頁
} UIViewAnimationTransition;加減速曲線的可選值:
typedef enum {
UIViewAnimationCurveEaseInOut, // 先加速再減速,最自然
UIViewAnimationCurveEaseIn, // 起步慢
UIViewAnimationCurveEaseOut, // 收尾慢
UIViewAnimationCurveLinear // 等速
} UIViewAnimationCurve;[UIView beginAnimations:@"ddd" context:nil];
view.frame = CGRectMake(200, 200, 100, 100);
[UIView commitAnimations];當 view 從原本的 frame 移往新的 frame 時,中間會有漸變過程,而不是瞬間跳到位。其他效果也可以疊加在中間,多種效果會同時發生。
以下這些都能寫在 begin 與 commit 之間:
// 位移
view.transform = CGAffineTransformMakeTranslation(10, 10); // 相對初始狀態,只能作用一次
view.transform = CGAffineTransformTranslate(view.transform, 10, 10); // 可連續累加
// 旋轉
self.view.transform = CGAffineTransformMakeRotation(M_PI); // 只能旋轉一次
self.view.transform = CGAffineTransformRotate(self.view.transform, M_PI); // 可累加
// 縮放(數值相對於原本的倍數)
self.view.transform = CGAffineTransformMakeScale(1.1, 1.1); // 只能改變一次
self.view.transform = CGAffineTransformScale(self.view.transform, 1.1, 1.1); // 可累加
// 還原
self.view.transform = CGAffineTransformIdentity; // 回到最初的樣子
self.view.transform = CGAffineTransformInvert(self.view.transform); // 得到相反的樣子Make 開頭的方法是以「初始狀態」為基準建立新矩陣,因此連續呼叫兩次只會保留最後一次;而 Translate、Rotate、Scale 這些動詞版本是在既有矩陣上「再疊加」,可以累積。這是很多人搞錯的地方。
[UIView animateWithDuration:3 animations:^{
// 這裡相當於 begin 與 commit 之間
} completion:^(BOOL finished){
// 動畫執行完成後要做的事,還可以再嵌套 block
}];Block 語法比 begin/commit 好讀得多,也更容易看出動畫的邊界在哪裡,新專案建議一律使用這種寫法。
使用前需加入 QuartzCore 庫並引入標頭檔。CAAnimation 有三個主要子類。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
// keyPath 有許多可選值,一定要填對,動畫才會執行
// opacity 設定透明度,bounds.size 設定大小
[animation setFromValue:[NSNumber numberWithFloat:1.0]]; // 起始值
[animation setToValue:[NSNumber numberWithFloat:0.3]]; // 結束值
[animation setDuration:0.1]; // 動畫時間
[animation setRepeatCount:100000]; // 重複次數
[animation setRepeatDuration:4]; // 重複總時長(會限制次數)
[animation setAutoreverses:NO]; // 是否 1.0→0.3→1.0 算一次;設 NO 則 1.0→0.3 為一次
[animation setRemovedOnCompletion:YES]; // 完成時移出動畫(預設即為 YES)
[view.layer addAnimation:animation forKey:@"abc"]; // 執行動畫CAKeyframeAnimation *animation =
[CAKeyframeAnimation animationWithKeyPath:@"position"];
NSArray *postionAraay = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(100, 20)],
[NSValue valueWithCGPoint:CGPointMake(40, 80)],
[NSValue valueWithCGPoint:CGPointMake(30, 60)],
[NSValue valueWithCGPoint:CGPointMake(20, 40)],
[NSValue valueWithCGPoint:CGPointMake(0, 100)],
nil];
NSArray *times = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.3],
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:0.1],
[NSNumber numberWithFloat:1.0],
nil];
[animation setKeyTimes:times];
[animation setValues:postionAraay];
[animation setDuration:5];
[bigImage.layer addAnimation:animation forKey:@"dd"];keyframe 動畫適合路徑不規則的移動。keyTimes 的值必須從 0 到 1 遞增,代表每個關鍵點在整段動畫中的時間佔比。
CATransition *animation = [CATransition animation];
animation.duration = 0.5f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
/*
自帶的 transition 類型:
kCATransitionFade 淡入淡出
kCATransitionMoveIn 移入
kCATransitionPush 推入
kCATransitionReveal 揭開
*/
/*
方向:
kCATransitionFromRight
kCATransitionFromLeft
kCATransitionFromTop
kCATransitionFromBottom
*/
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromBottom;
[view.layer addAnimation:animation forKey:animation];type 也可以直接傳字串,如此一來就能使用非公開的私有轉場效果:
cube 立方體翻轉
suckEffect 捲走
oglFlip 翻轉
rippleEffect 水波
pageCurl 翻頁
pageUnCurl 反翻頁
cameraIrisHollowOpen / cameraIrisHollowClose 相機光圈開闔但要提醒的是,這些字串對應的是私有實作,Apple 未公開保證其行為,使用它們提交 App 審核存在被拒的風險,正式產品請只用上面列出的四個公開常量。