萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS 動畫大師之路:UIView 動畫 API 全方位詳解與實戰

核心概念:動畫區塊

UIView 動畫的本質是「將屬性變更包裝在動畫區塊中」,由 UIKit 自動計算插值並渲染。經典 API 流程:

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// 這裡修改可動畫屬性(frame、alpha、transform、backgroundColor 等)
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];

完整 API 參考表

方法用途關鍵參數
beginAnimations:context:開始動畫區塊animationID(識別碼)、context(傳遞給代理)
commitAnimations提交並啟動動畫—
setAnimationDuration:設定持續時間(秒)預設 0.2
setAnimationDelay:設定延遲啟動(秒)預設 0.0
setAnimationCurve:設定緩動曲線EaseInOut(預設)、EaseIn、EaseOut、Linear
setAnimationTransition:forView:cache:設定轉場效果Flip、Curl、CrossDissolve 等
setAnimationDelegate:設定代理物件接收 start/stop 回調
setAnimationWillStartSelector:動畫開始回調selector 簽名需含 animationID、context
setAnimationDidStopSelector:動畫結束回調selector 簽名:animationFinished:finished:context:
setAnimationRepeatCount:重複次數可為小數,預設 0(不重複)
setAnimationRepeatAutoreverses:來回重複YES/NO,需配合 repeatCount
setAnimationBeginsFromCurrentState:從當前狀態開始YES:打斷當前動畫無縫銜接
setAnimationStartDate:指定開始時間NSDate,預設立即
areAnimationsEnabled查詢動畫是否啟用回傳 BOOL
setAnimationsEnabled:全域啟用/停用動畫YES/NO

實戰範例:兩個 View Controller 翻轉切換

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.blueController = [[BlueViewController alloc] initWithNibName:nil bundle:nil];
    self.blueController.view.frame = self.view.bounds;
    
    self.yellowController = [[YellowController alloc] initWithNibName:nil bundle:nil];
    self.yellowController.view.frame = self.view.bounds;
    
    [self.view insertSubview:self.blueController.view atIndex:0];
    [self.view insertSubview:self.yellowController.view atIndex:1];  // 最上層
    
    self.rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Next"
                                                         style:UIBarButtonItemStylePlain
                                                        target:self
                                                        action:@selector(buttonPressed)];
    self.navigationItem.rightBarButtonItem = self.rightBarItem;
}

- (void)buttonPressed {
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView commitAnimations];
}

常見坑與解決方案

問題:動畫寫在 viewDidLoad 不執行

原因: App 啟動時系統有預設動畫,兩者衝突導致自訂動畫被吞噬。

解決:

  1. 移至按鈕事件或使用者互動後觸發
  2. 或用 performSelector:withObject:afterDelay: 延遲執行
  3. 或用 NSTimer 延遲一幀

現代替代方案:Block-based API(iOS 4+)

舊版 API 已不建議新專案使用。現代寫法更簡潔、支援完成閉包:

[UIView animateWithDuration:1.25
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCurlUp
                 animations:^{
                     [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
                 }
                 completion:^(BOOL finished) {
                     // 動畫結束後處理
                 }];

圖層動畫

layerClass 可自訂 View 底層 CALayer 類別(預設 CALayer),進階動畫建議直接操作 CALayer 或使用 CAAnimation 子類別。

copyright © 萬盛學電腦網 all rights reserved