頁面之間硬切太生硬,加上轉場動畫就能立刻拉開質感。iOS 提供了 CATransition 這個方便的工具,一行設定就能換一種效果。本文把常見的轉場類型整理成一份可直接取用的清單。
先把重複的樣板收進一個方法裡,之後只要傳入 type 與 subtype 就能切換效果:
#pragma mark CATransition 動畫實作
- (void)transitionWithType:(NSString *)type WithSubtype:(NSString *)subtype ForView:(UIView *)view {
// 建立 CATransition 物件
CATransition *animation = [CATransition animation];
// 設定動畫時間
animation.duration = DURATION;
// 設定運動類型
animation.type = type;
if (subtype != nil) {
// 設定子類型(方向)
animation.subtype = subtype;
}
// 設定運動節奏
animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
[view.layer addAnimation:animation forKey:@"animation"];
}
系統預設五種節奏:
kCAMediaTimingFunctionLinear:線性,即勻速kCAMediaTimingFunctionEaseIn:先慢後快kCAMediaTimingFunctionEaseOut:先快後慢kCAMediaTimingFunctionEaseInEaseOut:先慢後快再慢kCAMediaTimingFunctionDefault:實際表現為中段較快CATransition 能做的效果有限,有些轉場(例如翻頁)得改用 UIView 的動畫 API:
#pragma mark 以 UIView 動畫實作轉場
- (void)animationWithView:(UIView *)view WithAnimationTransition:(UIViewAnimationTransition)transition {
[UIView animateWithDuration:DURATION animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:transition forView:view cache:YES];
}];
}
這裡的 cache:YES 值得留意:它會先將畫面快取成點陣圖再進行動畫,通常較順,但若轉場期間內容有變動就會出現殘影。
#pragma mark 為 view 加上背景圖
- (void)addBgImageWithImageName:(NSString *)imageName {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];
}
在 view 上放多顆按鈕,各自設定不同的 tag,然後在 viewController 裡綁定同一個方法。點哪一顆,就播放哪一種轉場。

typedef enum : NSUInteger {
Fade = 0, // 淡入淡出
Push, // 推擠
Reveal, // 揭開
MoveIn, // 覆蓋
Cube, // 立方體
SuckEffect, // 吮吸
OglFlip, // 翻轉
RippleEffect, // 波紋
PageCurl, // 翻頁
PageUnCurl, // 反翻頁
CameraIrisHollowOpen, // 開鏡頭
CameraIrisHollowClose, // 關鏡頭
CurlDown, // 下翻頁
CurlUp, // 上翻頁
FlipFromLeft, // 左翻轉
FlipFromRight // 右翻轉
} AnimationType;
UIButton *button = sender; AnimationType animationType = button.tag;
NSString *subtypeString;
switch (_subtype) {
case 0: subtypeString = kCATransitionFromLeft; break;
case 1: subtypeString = kCATransitionFromBottom; break;
case 2: subtypeString = kCATransitionFromRight; break;
case 3: subtypeString = kCATransitionFromTop; break;
default: break;
}
_subtype += 1;
if (_subtype > 3) {
_subtype = 0;
}
switch (animationType) {
case Fade:
[self transitionWithType:kCATransitionFade WithSubtype:subtypeString ForView:self.view];
break;
case Push:
[self transitionWithType:kCATransitionPush WithSubtype:subtypeString ForView:self.view];
break;
case Reveal:
[self transitionWithType:kCATransitionReveal WithSubtype:subtypeString ForView:self.view];
break;
case MoveIn:
[self transitionWithType:kCATransitionMoveIn WithSubtype:subtypeString ForView:self.view];
break;
case Cube:
[self transitionWithType:@"cube" WithSubtype:subtypeString ForView:self.view];
break;
case SuckEffect:
[self transitionWithType:@"suckEffect" WithSubtype:subtypeString ForView:self.view];
break;
case OglFlip:
[self transitionWithType:@"oglFlip" WithSubtype:subtypeString ForView:self.view];
break;
case RippleEffect:
[self transitionWithType:@"rippleEffect" WithSubtype:subtypeString ForView:self.view];
break;
case PageCurl:
[self transitionWithType:@"pageCurl" WithSubtype:subtypeString ForView:self.view];
break;
case PageUnCurl:
[self transitionWithType:@"pageUnCurl" WithSubtype:subtypeString ForView:self.view];
break;
case CameraIrisHollowOpen:
[self transitionWithType:@"cameraIrisHollowOpen" WithSubtype:subtypeString ForView:self.view];
break;
case CameraIrisHollowClose:
[self transitionWithType:@"cameraIrisHollowClose" WithSubtype:subtypeString ForView:self.view];
break;
// 以下四種需改用 UIView 動畫 API
case CurlDown:
[self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlDown];
break;
case CurlUp:
[self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlUp];
break;
case FlipFromLeft:
[self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromLeft];
break;
case FlipFromRight:
[self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromRight];
break;
}
淡入淡出(Fade)

推擠(Push)
新畫面把舊畫面推走,導航返回時最常見的一種。
揭開(Reveal)

覆蓋(MoveIn)

立方體(Cube)

吮吸(SuckEffect)

翻轉(OglFlip)

波紋(RippleEffect)

翻頁與反翻頁(PageCurl / PageUnCurl)


相機光圈(CameraIrisHollowOpen / Close)

效果多,不代表都該用。実務上真正耐看的是 Fade、Push、MoveIn 這幾種;Cube、SuckEffect、RippleEffect 這類華麗效果適合用在少數關鍵節點,全程濫用反而讓人眼花。同一個 App 內的轉場風格最好保持一致,這比效果多寡更能體現設計功力。