您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 動畫實作:UIView 過場動畫與 CATransition 完整用法

編輯:iOS 開發綜合

做 iOS 動畫時,最直覺的兩條路是 UIView 的動畫區塊與 Core Animation 的 CATransition。前者寫起來短,後者能控制的細節多。這篇把兩邊的常用寫法都整理出來。

一、用 UIView 動畫做翻轉與捲頁

以四個方向按鈕為例,左右是翻轉,上下是捲軸:

- (void)leftClick {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 起步與收尾較慢
    [UIView setAnimationDuration:1.0f];                       // 動畫時長
    [UIView setAnimationBeginsFromCurrentState:YES];          // 承接目前狀態,避免跳動
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:imageView cache:YES];
    [UIView commitAnimations];
}

- (void)rightClick {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:imageView cache:YES];
    [UIView commitAnimations];
}

- (void)upClick {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                           forView:imageView cache:YES];
    [UIView commitAnimations];
}

- (void)downClick {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
                           forView:imageView cache:YES];
    [UIView commitAnimations];
}

setAnimationBeginsFromCurrentState:YES 值得特別留意:如果上一段動畫還沒跑完使用者又觸發新的,這一行能讓新動畫從目前的中間狀態接續,而不是硬跳回起始點。

二、CATransition:更可控的過場

CATransition 的功能大致能模擬上面那些效果,但多了方向、進度、是否移除等控制項。

- (void)MyCAnimation1 {
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0f;                       // 動畫時長
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionMoveIn;            // 新視圖移到舊視圖之上
    animation.subtype = kCATransitionFromTop;        // 方向
    [imageView.layer addAnimation:animation forKey:nil];
}

- (void)MyCAnimation2 {
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.removedOnCompletion = NO;              // 動畫結束後仍保留
    animation.type = @"pageCurl";                    // 向上翻頁
    animation.subtype = kCATransitionFromRight;
    animation.fillMode = kCAFillModeForwards;
    animation.endProgress = 0.7;                     // 只播放到七成
    [imageView.layer addAnimation:animation forKey:nil];
}

- (void)MyCAnimation3 {
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = @"pageUnCurl";                  // 向下翻頁
    animation.subtype = kCATransitionFromRight;
    animation.fillMode = kCAFillModeBackwards;
    animation.startProgress = 0.3;                   // 從三成處開始
    [imageView.layer addAnimation:animation forKey:nil];
}

type 與 subtype 有哪些值

官方以 #define 常數提供的四種基本型別:

  • kCATransitionFade:交叉淡化
  • kCATransitionMoveIn:新視圖移到舊視圖之上
  • kCATransitionPush:新視圖把舊視圖推出去
  • kCATransitionReveal:舊視圖移開,露出底下的新視圖

而網路文章常見的字串型別(例如 pageCurl、pageUnCurl、rippleEffect、suckEffect、cube、oglFlip、cameraIrisHollowOpen、cameraIrisHollowClose)並未完整列入公開標頭,屬於未公開的實作細節。它們在某些系統版本上確實看得到效果,但上架審核與跨版本相容性都有風險,正式專案請優先選用官方常數。

方向則有四種:kCATransitionFromRight、kCATransitionFromLeft、kCATransitionFromTop、kCATransitionFromBottom。其中 fade、suckEffect、rippleEffect 與鏡頭類效果不支援方向設定。

三、一個完整的 cube 範例

CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.5f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.type = @"cube";                 // 立方體翻滾
animation.subtype = @"fromLeft";          // 方向
animation.startProgress = 0.0;            // 起始百分比
animation.endProgress = 1.0;              // 結束百分比
animation.removedOnCompletion = NO;
[self.view.layer addAnimation:animation forKey:@"animation"];

補充一個常被誤寫的細節:timingFunction 的型別是 CAMediaTimingFunction,直接塞 UIViewAnimationCurve 的列舉值並不嚴謹,正式寫法應該是 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]。

四、順手做個定時動畫

如果想讓某個元件每隔一段時間換個位置,用計時器搭配動畫屬性即可:

- (void)MyCAnimation4 {
    [NSTimer scheduledTimerWithTimeInterval:3.0f
                                     target:self
                                   selector:@selector(updateButterfly)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)updateButterfly {
    butterflyView.animationDuration = 0.75f;
    [self.view addSubview:butterflyView];
    [butterflyView startAnimating];
    butterflyView.center = [butterflyView randomCenterInView:self.view withInset:10.0f];
}

整體來說,日常需求用 UIView 動畫就足夠,需要更細的過場控制或想直接操作 layer 時,再換成 CATransition。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved