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

iOS 動畫實作總整理:Core Animation 與 UIView Animation

編輯:iOS 開發綜合

iOS 的動畫大致分兩條路:操作 UIView 的 animation 系列方法,以及直接使用核心動畫(Core Animation)。從 iOS 7 開始,UIView 動畫底層其實就是核心動畫,兩者並非毫無關聯。以下把常用寫法整理成清單。

方式一:核心動畫

核心動畫的類別關係大致如下:

  • CAAnimation
    • CATransition:場景轉場
    • CAAnimationGroup:動畫組
    • CAPropertyAnimation
      • CABasicAnimation:基礎動畫
      • CAKeyframeAnimation:幀動畫

CATransition:轉場動畫

用於場景切換,可為圖層提供移出/移入螢幕的效果。UINavigationController 的推入動畫正是靠它實作。

常用屬性:

  • type:轉場類型
  • subtype:轉場方向
  • startProgress:動畫起點(佔整體的百分比,0~1)
  • endProgress:動畫終點(起點數值必須小於終點)

CABasicAnimation:基礎動畫

CAPropertyAnimation 的子類,一次控制一個屬性,在 fromValue 與 toValue 之間變化。

CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(20, 20, 100, 100)];
baseAnimation.toValue   = [NSValue valueWithCGRect:CGRectMake(100, 100, 200, 200)];
baseAnimation.duration  = 2.0;
baseAnimation.removedOnCompletion = NO;
baseAnimation.fillMode  = kCAFillModeForwards;
baseAnimation.repeatCount = MAXFLOAT;
[self.myView.layer addAnimation:baseAnimation forKey:nil];

CAKeyframeAnimation:幀動畫

同樣是 CAPropertyAnimation 的子類,差別在於可以設定多個關鍵幀。CABasicAnimation 可以理解成只有兩個關鍵幀的特例。利用多關鍵幀能做出泡泡浮動這類效果。

CAAnimationGroup *group = [[CAAnimationGroup alloc] init];

// 位移:沿圓形路徑移動
CAKeyframeAnimation *positionAnima =
    [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnima.calculationMode = kCAAnimationCubicPaced;
positionAnima.duration = 5;
positionAnima.fillMode = kCAFillModeForwards;
positionAnima.removedOnCompletion = NO;
positionAnima.repeatCount = MAXFLOAT;
positionAnima.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

CGMutablePathRef curvedPath = CGPathCreateMutable();
CGRect circleContainer = CGRectInset(myView.frame,
    myView.frame.size.width  / 2 - 3,
    myView.frame.size.height / 2 - 3);
CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
positionAnima.path = curvedPath;
CGPathRelease(curvedPath);
[myView.layer addAnimation:positionAnima forKey:nil];

// 水平縮放
CAKeyframeAnimation *scaleX =
    [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
scaleX.duration = 1.0;
scaleX.values = @[@1.0, @1.1, @1.0];
scaleX.keyTimes = @[@0.0, @0.5, @1.0];
scaleX.repeatCount = MAXFLOAT;
scaleX.autoreverses = YES;
scaleX.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:scaleX forKey:nil];

// 垂直縮放
CAKeyframeAnimation *scaleY =
    [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
scaleY.duration = 1.5;
scaleY.values = @[@1.0, @1.1, @1.0];
scaleY.keyTimes = @[@0.0, @0.5, @1.0];
scaleY.repeatCount = MAXFLOAT;
scaleY.autoreverses = YES;
scaleY.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:scaleY forKey:nil];

group.animations = @[positionAnima, scaleX, scaleY];

CAAnimationGroup:動畫組

可以保存一組動畫物件,加入圖層後同時運行。當多個動畫時長不一致時,動畫組尤其方便——上面的泡泡效果就是例子。

group.animations = @[ /* 放入各個動畫物件 */ ];

方式二:UIView 動畫

手寫方式:單個動畫

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
CGPoint point = self.myView.center;
point.y += 150;
[self.myView setCenter:point];
[UIView commitAnimations];

手寫方式:多個動畫

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
CGPoint point = self.myView.center;
point.y += 150;
[self.myView setCenter:point];
[UIView commitAnimations];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:4];
[self.myView setAlpha:0.1];
[UIView commitAnimations];

Block 方式(推薦)

[UIView animateWithDuration:4 animations:^{
    CGPoint point = self.myView.center;
    point.y += 150;
    [self.myView setCenter:point];
}];

Block 幀動畫(iOS 7 起)

蘋果提供了便捷方法,不必自行建立幀動畫實例,直接控制屬性即可。

[UIView animateKeyframesWithDuration:0.5
                               delay:1
                             options:UIViewKeyframeAnimationOptionAutoreverse
                          animations:^{
    self.view.bounds = CGRectMake(30, 30, 30, 30);
} completion:^(BOOL finished) {
}];

轉場動畫

+ (void)transitionFromView:(UIView *)fromView
                    toView:(UIView *)toView
                  duration:(NSTimeInterval)duration
                   options:(UIViewAnimationOptions)options
                completion:(void (^)(BOOL finished))completion

這個方法呼叫完成後,效果等同於執行以下兩句:

[fromView.superview addSubview:toView];
[fromView removeFromSuperview];

參數說明:

  • duration:動畫持續時間
  • options:轉場動畫的類型
  • completion:動畫結束後自動呼叫的區塊
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved