很多 App 的引導頁會用「圖片緩慢放大 + 淡入淡出切換」的效果來提升質感。本文示範如何在啟動頁放三張重疊的 UIImageView,再用 NSTimer 與 CATransition 讓它們依序淡入、放大、交換層序,形成循環播放的動畫。
做法可以拆成三個環節:
CATransition 的淡入效果搭配 exchangeSubviewAtIndex:withSubviewAtIndex: 交換層序,讓下一張圖自然地浮上來。三個步驟透過計時器串成一個循環:第一張 → 第二張 → 第三張 → 回到第一張。
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
{
NSTimer *timer;
UIImageView *imageView1;
UIImageView *imageView2;
UIImageView *imageView3;
UIView *view1;
UIView *view2;
UIView *view3;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[self initBackgroundView];
}
#pragma mark - functions
- (void)initBackgroundView
{
// 第一張圖片
view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[self.view addSubview:view1];
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 420, 560)];
imageView1.image = [UIImage imageNamed:@"guidex1.png"];
[view1 addSubview:imageView1];
// 第二張圖片
view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[self.view addSubview:view2];
imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 420, 560)];
imageView2.image = [UIImage imageNamed:@"guidex2.png"];
[view2 addSubview:imageView2];
// 第三張圖片
view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 560)];
[self.view addSubview:view3];
imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 520, 660)];
imageView3.image = [UIImage imageNamed:@"guidex0.png"];
[view3 addSubview:imageView3];
// 啟動動畫事件
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(changeView)
userInfo:nil
repeats:NO];
}
// 三個「縮放」動畫,duration 刻意拉長讓節奏變慢
- (void)change
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:11.0];
imageView1.frame = CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];
}
- (void)change1
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:11.0];
imageView2.frame = CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];
}
- (void)change2
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:11.0];
imageView3.frame = CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];
}
// 第一張退場,第三張登場
- (void)changeView
{
imageView1.frame = CGRectMake(0, 0, 520, 660);
[self change2];
CATransition *transition = [CATransition animation];
transition.duration = 2.0; // 轉場時間
transition.timingFunction = UIViewAnimationCurveEaseInOut; // 緩動曲線
transition.type = kCATransitionFade; // 淡入淡出
transition.subtype = kCATransitionFromRight;
transition.delegate = self; // 動畫代理,自帶方法
[self.view.layer addAnimation:transition forKey:nil];
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(changeView1)
userInfo:nil
repeats:NO];
}
// 第二張退場,第一張登場
- (void)changeView1
{
imageView2.frame = CGRectMake(0, 0, 520, 660);
[self change];
CATransition *transition = [CATransition animation];
transition.duration = 2.0;
transition.timingFunction = UIViewAnimationCurveEaseInOut;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(changeView2)
userInfo:nil
repeats:NO];
}
// 第三張退場,第二張登場,回到起點形成循環
- (void)changeView2
{
imageView3.frame = CGRectMake(0, 0, 520, 660);
[self change1];
CATransition *transition = [CATransition animation];
transition.duration = 2.0;
transition.timingFunction = UIViewAnimationCurveEaseInOut;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:0];
timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(changeView)
userInfo:nil
repeats:NO];
}
這裡的縮放動畫時長設為 11 秒、切換間隔也是 11 秒,兩者刻意對齊,讓「放大」與「換圖」在同一個時間點完成交接,視覺上才不會突兀。實際專案中通常會把這些魔法數字抽成常數,方便統一調整節奏。
另外要留意,範例中的 frame 數值(320 × 460、520 × 660)是舊螢幕尺寸的寫死值。若要在多尺寸裝置上呈現一致效果,建議改用 Auto Layout 或根據 [[UIScreen mainScreen] bounds] 動態計算。
最後,這套邏輯裡的計時器一路遞迴排下去,若頁面被關閉而沒有 invalidate,計時器與其 target 都無法釋放。記得在 viewWillDisappear: 或 dealloc 中補上 [timer invalidate];。