頁面切換效果想做得好看,核心就是 CATransition。本文把常用的轉場型別整理出來,並封裝成兩個方法:一個走 Core Animation,一個走 UIView 的 block API,之後只要換參數就能套用。
#pragma mark - CATransition 動畫
- (void)transitionWithType:(NSString *)type
WithSubtype:(NSString *)subtype
ForView:(UIView *)view {
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"];
}
#pragma mark - UIView 動畫
- (void)animationWithView:(UIView *)view
WithAnimationTransition:(UIViewAnimationTransition)transition {
[UIView animateWithDuration:DURATION animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:transition forView:view cache:YES];
}];
}
#pragma mark - 為 View 加上背景圖
-(void)addBgImageWithImageName:(NSString *)imageName {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];
}
在視圖上放多顆 Button,各自設定不同的 tag,並全部綁到同一個方法,就能用 tag 決定要播放哪一種轉場。

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;
// 每次點擊輪替 subtype
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; }
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;
效果預覽:










實務提醒:轉場動畫同一時間只建議有一個在跑,快速連續點擊容易出現畫面閃爍,可在動畫期間暫時停用使用者互動。