動畫是提升 App 質感最直接的武器。QQ、微信、新浪微博之所以用起來順手,很大程度上來自細節動畫的打磨。本文系統整理 UIView 層級的動畫做法(不含 Core Animation 的核心動畫),從最傳統的寫法一路談到 iOS 7 之後的關鍵幀動畫。
基本結構是把要動畫化的程式碼夾在 Begin 與 Commit 之間:
[UIView beginAnimations:nil context:nil]; // 要動畫化的程式碼 [UIView commitAnimations];
實際例子——讓 imageView 往下移動 150pt:
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:10.0]; CGPoint point = _imageView.center; point.y += 150; [_imageView setCenter:point]; [UIView commitAnimations];
要「同時」跑多個效果,就寫在同一組 Begin/Commit 之間:
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:3.0]; [_imageView setAlpha:0.0]; [UIView commitAnimations]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:3.0]; CGPoint point = _imageView.center; point.y += 150; [_imageView setCenter:point]; [UIView commitAnimations];
以上效果為:圖像向下平移 150pt,同時透明度設為 0。
也可以指定圖形上下文:
CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationDuration:2.0]; [_imageView setAlpha:0]; [UIView commitAnimations];
UIGraphicsGetCurrentContext() 取得的是當前視圖的繪圖上下文。
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 開始動畫
+ (void)commitAnimations; // 提交動畫
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // 動畫曲線,預設線性
+ (void)setAnimationDuration:(NSTimeInterval)duration; // 動畫時長
+ (void)setAnimationsEnabled:(BOOL)enabled; // 預設 YES;設 NO 則直接跳到結果狀態
+ (void)setAnimationDelay:(NSTimeInterval)delay; // 延遲秒數
+ (void)setAnimationRepeatCount:(float)repeatCount; // 重複次數
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // 是否來回播放,預設 NO
+ (void)setAnimationDelegate:(id)delegate; // 動畫代理
+ (void)setAnimationWillStartSelector:(SEL)selector; // 動畫將開始(需先設代理)
+ (void)setAnimationDidStopSelector:(SEL)selector; // 動畫已結束(需先設代理)
// 轉場效果;cache 為 YES 時首尾視圖各渲染一次並於動畫中重用影格
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition
forView:(UIView *)view cache:(BOOL)cache;
- (void)removeAllAnimations; // 移除所有動畫層,例如 [_imageView.layer removeAllAnimations];
方法一:最精簡
[UIView animateWithDuration:4.0 animations:^{ /* code */ }];
方法二:帶完成回呼
[UIView animateWithDuration:4.0 animations:^{ /* code */ }
completion:^(BOOL finished) { /* code */ }];
方法三:加上 delay 與 options
[UIView animateWithDuration:4.0
delay:2.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{ /* code */ }
completion:^(BOOL finished) { /* code */ }];
從 iOS 7 開始,系統動畫大量採用 Spring Animation:
[UIView animateWithDuration:4.0
delay:0.0
usingSpringWithDamping:1.0 // 0~1,數值越小彈跳感越明顯
initialSpringVelocity:5.0 // 初始速度,越大開場越快
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
}
completion:^(BOOL finished) {
[_imageView setAlpha:1];
}];
為什麼要用它?因為 iOS 系統本身大量使用 Spring Animation,使用者早已習慣這種手感,用 Apple 的說法就是「instantly familiar」。此外它不只用於位置,凡是可動畫化的屬性都適用。要留意的是:當初始速度較高而 duration 較短時,反而會出現過度反彈的情況。
iOS 7 之後加入的 animateKeyframesWithDuration: 讓我們不必動用 Core Animation,也能做出多段式、更複雜的動畫。
// 建立關鍵幀動畫
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewKeyframeAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
// 加入單一關鍵幀
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
relativeDuration:(double)frameDuration
animations:(void (^)(void))animations;
這裡的時間是「相對值」,系統會依動畫總時長自動換算成實際執行時間。
以「彩虹變色視圖」為例:
void (^keyFrameBlock)() = ^{
NSArray *arrayColors = @[[UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor],
[UIColor blueColor], [UIColor purpleColor], [UIColor redColor]];
NSUInteger colorCount = [arrayColors count];
for (NSUInteger i = 0; i < colorCount; i++) {
[UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
relativeDuration:1 / (CGFloat)colorCount
animations:^{
[_graduallyView setBackgroundColor:arrayColors[i]];
}];
}
};
[UIView animateKeyframesWithDuration:4.0
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear
animations:keyFrameBlock
completion:^(BOOL finished) { /* code */ }];
新增的計算模式選項:
UIViewKeyframeAnimationOptionCalculationModeLinear = 0 << 10, // 預設 UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10, UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10, UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10, UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10

UIView 提供的動畫方式相當多:單純的位移、淡入淡出用 animateWithDuration: 就綽綽有餘;需要多段、有節奏變化或序列式變化的效果,改用 Keyframe 會乾淨很多。至於要不要動用 Core Animation,取決於是否需要更細粒度的控制(例如逐屬性的 timing function、3D 轉場、粒子效果)。最終還是要回到產品需求來判斷。
— 本文作者 GarveyCalvin,原文發表於 https://www.cnblogs.com/GarveyCalvin/