做 iOS 動畫時,最直覺的兩條路是 UIView 的動畫區塊與 Core Animation 的 CATransition。前者寫起來短,後者能控制的細節多。這篇把兩邊的常用寫法都整理出來。
以四個方向按鈕為例,左右是翻轉,上下是捲軸:
- (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 的功能大致能模擬上面那些效果,但多了方向、進度、是否移除等控制項。
- (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];
}
官方以 #define 常數提供的四種基本型別:
kCATransitionFade:交叉淡化kCATransitionMoveIn:新視圖移到舊視圖之上kCATransitionPush:新視圖把舊視圖推出去kCATransitionReveal:舊視圖移開,露出底下的新視圖而網路文章常見的字串型別(例如 pageCurl、pageUnCurl、rippleEffect、suckEffect、cube、oglFlip、cameraIrisHollowOpen、cameraIrisHollowClose)並未完整列入公開標頭,屬於未公開的實作細節。它們在某些系統版本上確實看得到效果,但上架審核與跨版本相容性都有風險,正式專案請優先選用官方常數。
方向則有四種:kCATransitionFromRight、kCATransitionFromLeft、kCATransitionFromTop、kCATransitionFromBottom。其中 fade、suckEffect、rippleEffect 與鏡頭類效果不支援方向設定。
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。