iOS基本動畫/主要畫面格動畫/利用easing 函式實現物理動畫效果

來源:互聯網
上載者:User

標籤:code   利用   添加   out   地址   view   使用   href   ati   

先說下基本動畫部分

基本動畫部分比較簡單, 但能實現的動畫效果也很局限

使用方法大致為:

#1. 建立原始UI或者畫面

#2. 建立CABasicAnimation執行個體, 並設定keypart/duration/fromValue/toValue

#3. 設定動畫最終停留的位置

#4. 將配置好的動畫添加到layer層中

舉個例子, 比如實現一個圓形從上往下移動, 上代碼:

 1 //設定原始畫面 2     UIView *showView               = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 3     showView.layer.masksToBounds   = YES; 4     showView.layer.cornerRadius    = 50.f; 5     showView.layer.backgroundColor = [UIColor redColor].CGColor;
6 [self.view addSubview:showView]; 7 8 //建立基本動畫 9 CABasicAnimation *basicAnimation = [CABasicAnimation animation];10 11 //設定屬性12 basicAnimation.keyPath = @"position";13 basicAnimation.duration = 4.0f;14 basicAnimation.fromValue = [NSValue valueWithCGPoint:showView.center];15 basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(50, 300)];16 17 //設定動畫結束位置18 showView.center = CGPointMake(50, 300);19 20 //添加動畫到layer層21 [showView.layer addAnimation:basicAnimation forKey:nil];

 

接下來說下主要畫面格動畫

其實跟基本動畫差不多, 只是能設定多個動畫路徑  使用方法也類似, 大致為

#1. 建立原始UI或者畫面

#2. 建立CAKeyframeAnimation執行個體, 並設定keypart/duration/values 相比基本動畫只能設定開始和結束點, 主要畫面格動畫能添加多個動畫路徑點

#3. 設定動畫最終停留的位置

#4. 將配置好的動畫添加到layer層中

舉個例子, 紅色圓形左右晃動往下墜落 上代碼:

 1 //設定原始畫面 2     UIView *showView               = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 3     showView.layer.masksToBounds   = YES; 4     showView.layer.cornerRadius    = 50.f; 5     showView.layer.backgroundColor = [UIColor redColor].CGColor; 6      7     [self.view addSubview:showView]; 8      9     //建立主要畫面格動畫10     CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];11     12     //設定動畫屬性13     keyFrameAnimation.keyPath              = @"position";14     keyFrameAnimation.duration             = 4.0f;15     16     keyFrameAnimation.values = @[[NSValue valueWithCGPoint:showView.center],17                                  [NSValue valueWithCGPoint:CGPointMake(100, 100)],18                                  [NSValue valueWithCGPoint:CGPointMake(50, 150)],19                                  [NSValue valueWithCGPoint:CGPointMake(200, 200)]];20     21     //設定動畫結束位置22     showView.center = CGPointMake(200, 200);23     24     //添加動畫到layer層25     [showView.layer addAnimation:keyFrameAnimation forKey:nil];

 

最後是利用easing 函式配合主要畫面格動畫實現比較複雜的物理性動畫

先說說什麼是easing 函式, 就是有高人寫了一個庫可以計算出類比物理性動畫(比如彈簧效果)所要的路徑

Github地址: https://github.com/YouXianMing/EasingAnimation

具體有哪些動畫效果可看庫中的easing 函式查詢表, 簡單舉個小球落地的效果

上代碼:

 1 //設定原始畫面 2     UIView *showView               = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 3     showView.layer.masksToBounds   = YES; 4     showView.layer.cornerRadius    = 50.f; 5     showView.layer.backgroundColor = [UIColor redColor].CGColor; 6      7     [self.view addSubview:showView]; 8      9     //建立主要畫面格動畫10     CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];11     12     //設定動畫屬性13     keyFrameAnimation.keyPath              = @"position";14     keyFrameAnimation.duration             = 4.0f;15     
    //關鍵處, 在這裡使用的easing 函式計算點路徑16 keyFrameAnimation.values = [YXEasing calculateFrameFromPoint:showView.center17 toPoint:CGPointMake(50, 300)18 func:BounceEaseOut19 frameCount:4.0f * 30];20 21 //設定動畫結束位置22 showView.center = CGPointMake(50, 300);23 24 //添加動畫到layer層25 [showView.layer addAnimation:keyFrameAnimation forKey:nil];

 

iOS基本動畫/主要畫面格動畫/利用easing 函式實現物理動畫效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.