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

【投稿】iOS 三球旋轉載入動畫的實作拆解

編輯:iOS 開發基礎

本文為投稿文章,作者:鄭欽洪


一、起點:一本動畫聖經帶來的靈感

最近一直在啃 KITTEN 寫的《A Guide to iOS Animation》,這本書在 iOS 動畫開發圈裡幾乎是公認的必讀之作,翻起來常常一頁接一頁停不下來。書中有一個載入動畫特別有意思,可惜原書只給了成品,沒有交代實作思路。於是我決定自己動手把它拆開來研究,並在原有效果的基礎上補充一些個人的想法。

先仔細觀察這段動畫:畫面裡有三顆球,正中央那顆幾乎原地不動,另外兩顆則沿著固定軌跡互換位置,繞圈旋轉。理解這個結構之後,我們再來看程式碼該怎麼寫。

二、讓三顆球轉起來

1. 用 cornerRadius 畫圓

三顆球的半徑與顏色完全相同。這裡的關鍵是 CALayer 的 cornerRadius:只要把它設成邊長的一半,一個正方形視圖立刻變成圓形。

UIView *ball = [[UIView alloc] initWithFrame:CGRectMake(x, y, BALL_RADIUS, BALL_RADIUS)];
ball.layer.cornerRadius = BALL_RADIUS / 2;   // 正方形 → 正圓
ball.backgroundColor = self.ballColor;

2. 決定三顆球各自的横坐标

三顆球並排陳列,寬高一致,差別只在 x 值。以畫面中心為基準,左球往左偏一個半徑,右球往右偏半個半徑,中間那顆則略為靠左。

CGFloat centerPointY = HEIGHT / 2 - BALL_RADIUS * 0.5;
CGFloat centerPointX = WIDTH / 2;
CGPoint centerPoint = CGPointMake(centerPointX, centerPointY);

// ball_1:x 為 centerPoint.x - BALL_RADIUS
// ball_2:x 為 centerPoint.x - BALL_RADIUS * 0.5
// ball_3:x 為 centerPoint.x + BALL_RADIUS * 0.5
// 三者皆套用 cornerRadius = BALL_RADIUS / 2 並加入視圖

3. 用貝茲曲線造一條軌道

動畫裡的球顯然是沿著某個圓周跑的。實作上用 UIBezierPath 畫圓即可,但要注意 UIBezierPath 一次只能畫一段弧,所以要從 180° 畫到 360°,再從 0° 畫到 180°,最後把兩段弧拼接成完整的圓。

// 球 1 的軌道
UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:centerBall_1];
[path1 addArcWithCenter:centerPoint radius:BALL_RADIUS
              startAngle:M_PI endAngle:2 * M_PI clockwise:NO];

UIBezierPath *path1b = [UIBezierPath bezierPath];
[path1b addArcWithCenter:centerPoint radius:BALL_RADIUS
               startAngle:0 endAngle:M_PI clockwise:NO];
[path1 appendPath:path1b];   // 兩段弧合成一個圓

接著把這條路徑交給核心動畫的 CAKeyframeAnimation,讓球沿著 position 跑動:

CAKeyframeAnimation *anim1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim1.path = path1.CGPath;
anim1.removedOnCompletion = NO;
anim1.fillMode = kCAFillModeForwards;
anim1.calculationMode = kCAAnimationCubic;
anim1.repeatCount = 1;
anim1.duration = 1.4;
anim1.delegate = self;
anim1.autoreverses = NO;
anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.ball_1.layer addAnimation:anim1 forKey:@"animation"];

這樣球 1 就動起來了。

4. 球 3 的動畫

球 3 的弧線順序與球 1 相反:先 0° 到 180°,再 180° 到 360°。這裡有個容易踩的坑——不要再為球 3 設定 delegate。球 1 已經擔任了動畫代理,若重複設定,代理回呼會被觸發兩次。

UIBezierPath *path3 = [UIBezierPath bezierPath];
[path3 moveToPoint:centerBall_2];
[path3 addArcWithCenter:centerPoint radius:BALL_RADIUS startAngle:0 endAngle:M_PI clockwise:NO];

UIBezierPath *path3b = [UIBezierPath bezierPath];
[path3b addArcWithCenter:centerPoint radius:BALL_RADIUS startAngle:M_PI endAngle:M_PI * 2 clockwise:NO];
[path3 appendPath:path3b];

CAKeyframeAnimation *anim3 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim3.path = path3.CGPath;
anim3.removedOnCompletion = NO;
anim3.fillMode = kCAFillModeForwards;
anim3.calculationMode = kCAAnimationCubic;
anim3.repeatCount = 1;
anim3.duration = 1.4;
anim3.autoreverses = NO;
anim3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.ball_3.layer addAnimation:anim3 forKey:@"rotation"];

三、細節優化:從「能轉」到「像那麼回事」

目前的效果只是單純的繞圈位移,跟原始設計稿比起來味道差很多。

比對之下,我們少了三件事:

  • 旋轉時,球 1 與球 3 會先向兩側位移一段距離
  • 向外位移的同時,球會縮小
  • 旋轉結束回到原位時,尺寸也跟著回復

要補上這三段變化,得讓自己成為動畫的 delegate(也就是前面在球 1 上設定的 self),並在 animationDidStart: 裡動手腳。

a. 位移量怎麼算

self.ball_1.transform = CGAffineTransformMakeTranslation(-BALL_RADIUS, 0);
self.ball_3.transform = CGAffineTransformMakeTranslation(BALL_RADIUS, 0);

b. 位移的同時縮小

三顆球一起縮到 0.7 倍。順帶一提,位移與縮放前後的軌跡關係可以對照下圖理解。

self.ball_1.transform = CGAffineTransformScale(self.ball_1.transform, 0.7, 0.7);
self.ball_3.transform = CGAffineTransformScale(self.ball_3.transform, 0.7, 0.7);
self.ball_2.transform = CGAffineTransformScale(self.ball_2.transform, 0.7, 0.7);

c. 用 UIView 動畫把三段變化串起來

先縮小、外移,接著在 completion 裡把 transform 還原成 CGAffineTransformIdentity,一個完整的呼吸感就出來了。

[UIView animateWithDuration:0.3 delay:0.1
     options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
  animations:^{
    self.ball_1.transform = CGAffineTransformMakeTranslation(-BALL_RADIUS, 0);
    self.ball_1.transform = CGAffineTransformScale(self.ball_1.transform, 0.7, 0.7);
    self.ball_3.transform = CGAffineTransformMakeTranslation(BALL_RADIUS, 0);
    self.ball_3.transform = CGAffineTransformScale(self.ball_3.transform, 0.7, 0.7);
    self.ball_2.transform = CGAffineTransformScale(self.ball_2.transform, 0.7, 0.7);
  } completion:^(BOOL finished){
    [UIView animateWithDuration:0.3 delay:0.1
         options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
      animations:^{
        self.ball_1.transform = CGAffineTransformIdentity;
        self.ball_3.transform = CGAffineTransformIdentity;
        self.ball_2.transform = CGAffineTransformIdentity;
      } completion:NULL];
  }];

d. 讓它無限循環

在動畫停止的回呼裡重新啟動一次動畫,迴圈就成立了。

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    [self rotationAnimation];
}

四、加一層毛玻璃

系統自帶的 UIVisualEffectView 就足夠勝任,把它鋪滿整個視圖墊在球底下,載入時的質感立刻提升一個檔次。

UIVisualEffectView *bgView = [[UIVisualEffectView alloc]
    initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
bgView.alpha = 0.9f;
bgView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
bgView.layer.cornerRadius = BALL_RADIUS / 2;
bgView.clipsToBounds = YES;
[self addSubview:bgView];

五、後記

近來正從 Objective-C 轉向 Swift,途中也收集到不少很不錯的 Swift 動畫寫法,等學完再整理成 Objective-C 版本跟大家分享。做動畫的過程裡遇過不少幾何與物理上的難題,高中數學早還給老師了,理解起來偶爾頗為吃力,但就是愛折騰,希望能一次比一次做得更深一點。

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