iOS 的動畫大致可以分成兩類:屬性動畫與轉場動畫。前者改變的是同一個 view 的數值屬性,後者則替換 view 的呈現方式。iOS 4.0 是個分水嶺,之前用大括號式的 begin/commit 寫法,之後改為 Block 形式。
寫法是把動畫內容包在 beginAnimations 與 commitAnimations 之間:
[UIView beginAnimations:@"move" context:context]; [UIView setAnimationDuration:1]; // 動畫時間 [UIView setAnimationDelegate:self]; // 設定代理 [UIView setAnimationWillStartSelector:@selector(startAnimation:)]; [UIView setAnimationDidStopSelector:@selector(stopAnimation:)]; // [UIView setAnimationRepeatAutoreverses:YES]; CGPoint point = self.view1.center; self.view1.center = CGPointMake(point.x + 100, point.y); // self.view1.backgroundColor = [UIColor orangeColor]; [UIView commitAnimations];
可以動的 UIView 屬性有:
@property frame @property bounds @property center @property transform @property alpha @property backgroundColor @property contentStretch
[UIView beginAnimations:@"move" context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view3
cache:YES];
[UIView commitAnimations];
可用的轉場類型:
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
新的 API 把動畫敘述收進 Block,程式碼更集中也更容易讀:
// 屬性動畫
+ animateWithDuration:delay:options:animations:completion:
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:
// 轉場動畫
+ transitionWithView:duration:options:animations:completion:
+ transitionFromView:toView:duration:options:completion:
// 關鍵幀動畫
+ animateKeyframesWithDuration:delay:options:animations:completion:
+ addKeyframeWithRelativeStartTime:relativeDuration:animations:
// 系統轉場動畫
+ performSystemAnimation:onViews:options:animations:completion:
// 彈簧動畫(行為仍待完整測試)
+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:
options:animations:completion:
+ performWithoutAnimation:
QuartzCore 的動畫建立在 layer 之上,能控制的維度比 UIView 的方法更多。
// CABasicAnimation:在起訖值之間插值
CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
baseAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(400, 100)];
baseAnimation.duration = 3;
[self.view2.layer addAnimation:baseAnimation forKey:@"baseAnimation"];
// CAKeyframeAnimation:依關鍵幀序列播放
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.values = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(self.view2.center.x + 5, self.view2.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view2.center.x, self.view2.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view2.center.x - 5, self.view2.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view2.center.x, self.view2.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view2.center.x + 5, self.view2.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view2.center.x, self.view2.center.y)],
nil];
keyAnimation.keyTimes = @[@0.1, @0.2, @0.3, @0.4, @0.5, @1.0]; // 以百分比指定時間
keyAnimation.calculationMode = kCAAnimationDiscrete;
[self.view2.layer addAnimation:keyAnimation forKey:@"position"];
// CATransition:整體轉場
CATransition *transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.duration = 3;
transition.type = kCATransitionReveal; // 依值不同呈現不同效果
transition.subtype = kCATransitionFromRight;
[self.view.layer addAnimation:transition forKey:@"transition"];
日常需求優先用 UIView 的 Block 動畫,程式碼最少、效能已由系統最佳化。需要「播放完畢後做一件事」或「連續多段有先後順序」,改用 UIView animateKeyframesWithDuration:。只有當你要控制 timingFunction、自訂 CAAnimation 委派、或動畫 layer 層級屬性(如 contents、shadowPath)時,才需要下探到 CAAnimation。
最後要留意的是:CAAnimation 預設在動畫結束後會自動移除,且不改變 model layer 的值。若希望動畫停留在終點,必須同時設定 model layer 的屬性,並加上 removedOnCompletion = NO 與 fillMode = kCAFillModeForwards,否則畫面會「跳回原點」。