轉場動畫是讓 App 感覺「有質感」的關鍵細節之一。Core Animation 提供的 CATransition 能以極少的程式碼做出推入、翻頁、漣漪等效果,本文從屬性講起,最後搭配三個可直接套用的範例。

CATransition 是 CAAnimation 的子類,專門用來做轉場,能為圖層提供「移出螢幕」與「移入螢幕」的過渡效果。相較於 Mac OS X,iOS 提供的內建轉場類型略少一些。系統中的 UINavigationController 導航切換,正是靠 CATransition 把控制器視圖推入螢幕來實現的。
頭檔中值得關注的屬性:
#import <QuartzCore/CAAnimation.h> CATransition *myTransition = [CATransition animation]; // 建立 CATransition myTransition.duration = 0.3; // 持續 0.3 秒 myTransition.timingFunction = UIViewAnimationCurveEaseInOut; // 計時函數,控制整體流暢度 myTransition.type = kCATransitionPush; // 動畫類型 myTransition.subtype = kCATransitionFromLeft; // 子類型(方向)
要讓轉場生效,關鍵是把動畫加到「即將呈現動畫效果的那個圖層」上,選錯圖層就會看不到效果:
// 情況一:在兩個視圖控制器之間轉場 → 加到 window 的圖層 [[self.view.superview layer] addAnimation:myTransition forKey:nil]; // 情況二:控制器內的子視圖換成另一個子視圖 → 加到視圖控制器的圖層 [self.view.layer addAnimation:myTransition forKey:nil]; [self.view addSubview:newView]; [oldView removeFromSuperview]; // 情況三:使用導航控制器 → 加到 navigationController 的視圖圖層 [navigationController.view.layer addAnimation:myTransition forKey:nil];

除了 CATransition,UIView 也提供了兩個方便的類方法,省去手動操作圖層的麻煩。
1. 單視圖轉場
+ (void)transitionWithView:(UIView *)view
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
參數說明:duration 為持續時間,view 是目標視圖,options 指定轉場類型,animations 區塊中放要改變的視圖屬性,completion 在動畫結束後自動呼叫。
2. 雙視圖轉場
+ (void)transitionFromView:(UIView *)fromView
toView:(UIView *)toView
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
completion:(void (^)(BOOL finished))completion;
參數含義與上面類似,差別在於一次處理兩個視圖的交接。
使用前請牢記一個鐵則:轉場程式碼必須和動畫程式碼寫在一起,否則不生效。這是新手最常踩的坑——先把 image 換掉、之後才加動畫,畫面上只會看到圖片瞬間切換,沒有任何過渡。

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
// 注意:轉場程式碼必須和動畫程式碼寫在一起,否則無效
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/** 轉場程式碼:換圖片 */
static int index = 2;
NSString *imageName = [NSString stringWithFormat:@"%d", index];
_imageV.image = [UIImage imageNamed:imageName];
index++;
if (index == 4) {
index = 1;
}
/** 動畫程式碼 */
CATransition *anim = [CATransition animation];
anim.type = @"pageCurl"; // 翻頁類型
anim.subtype = kCATransitionFromLeft; // 方向
anim.duration = 3;
[_imageV.layer addAnimation:anim forKey:nil];
}
@end

這個效果用 UIView 自帶的單視圖轉場方法就能完成,示範中還示範了如何在動畫完成後串接下一段動畫:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) UIImageView *iconView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
[self.view addSubview:iconView];
iconView.center = self.view.center;
iconView.backgroundColor = [UIColor greenColor];
self.iconView = iconView;
// 設為圓角圖片
self.iconView.layer.cornerRadius = self.iconView.frame.size.width * 0.5;
self.iconView.clipsToBounds = YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView transitionWithView:self.iconView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
// 左翻轉,翻轉過程中把圖片換成 2
self.iconView.image = [UIImage imageNamed:@"2"];
} completion:^(BOOL finished) {
NSLog(@"completion");
// 左翻轉結束後停 1 秒,再執行右翻轉
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[UIView transitionWithView:self.iconView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
// 右翻轉,翻回來時顯示圖片 1
self.iconView.image = [UIImage imageNamed:@"1"];
} completion:nil];
});
}];
}
@end
使用雙視圖版本的轉場方法,可以讓兩個 view 在切換時產生翻轉過渡:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/** 視圖 1 */
@property (nonatomic, strong) UIView *view1;
/** 視圖 2 */
@property (nonatomic, strong) UIView *view2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// 1. scrollView 加入 view2
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor greenColor];
[self.scrollView addSubview:view2];
self.view2 = view2;
// 2. scrollView 加入 view1
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:view1];
self.view1 = view1;
// 3. 設定 frame
CGFloat scrollViewW = self.scrollView.frame.size.width;
CGFloat scrollViewH = self.scrollView.frame.size.height;
view1.frame = CGRectMake(0, 0, scrollViewW, scrollViewH);
view2.frame = CGRectMake(0, 0, scrollViewW, scrollViewH);
// 4. scrollView 設定
self.scrollView.contentSize = CGSizeMake(scrollViewW, scrollViewH);
// 加入點擊手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapClick:)];
[self.scrollView addGestureRecognizer:tap];
}
int i = 1;
// 點擊 scrollView 時來回翻轉
- (void)tapClick:(UITapGestureRecognizer *)tap {
if (i % 2 != 0) {
[UIView transitionFromView:self.view1
toView:self.view2
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromTop
completion:nil];
} else {
[UIView transitionFromView:self.view2
toView:self.view1
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:nil];
}
i++;
}
@end
三個範例對應三種常見場景:單一圖層換內容、單一視圖做 3D 翻面、兩個視圖互換。選對方法、把轉場與動畫程式碼放在一起,轉場效果基本就不會出錯了。