iOS基本動(dòng)畫/關(guān)鍵幀動(dòng)畫/利用緩動(dòng)函數(shù)實(shí)現(xiàn)物理動(dòng)畫效果

先說(shuō)下基本動(dòng)畫部分
基本動(dòng)畫部分比較簡(jiǎn)單, 但能實(shí)現(xiàn)的動(dòng)畫效果也很局限
使用方法大致為:

  1. 創(chuàng)建原始UI或者畫面
  2. 創(chuàng)建CABasicAnimation實(shí)例, 并設(shè)置keypart/duration/fromValue/toValue
  3. 設(shè)置動(dòng)畫最終停留的位置
  4. 將配置好的動(dòng)畫添加到layer層中
    舉個(gè)例子, 比如實(shí)現(xiàn)一個(gè)圓形從上往下移動(dòng), 上代碼:
//設(shè)置原始畫面
    UIView *showView               = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    showView.layer.masksToBounds   = YES;
    showView.layer.cornerRadius    = 50.f;
    showView.layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view addSubview:showView];
    
    //創(chuàng)建基本動(dòng)畫
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    
    //設(shè)置屬性
    basicAnimation.keyPath           = @"position";
    basicAnimation.duration          = 4.0f;
    basicAnimation.fromValue         = [NSValue valueWithCGPoint:showView.center];
    basicAnimation.toValue           = [NSValue valueWithCGPoint:CGPointMake(50, 300)];
    
    //設(shè)置動(dòng)畫結(jié)束位置
    showView.center = CGPointMake(50, 300);
    
    //添加動(dòng)畫到layer層
    [showView.layer addAnimation:basicAnimation forKey:nil];

接下來(lái)說(shuō)下關(guān)鍵幀動(dòng)畫
其實(shí)跟基本動(dòng)畫差不多, 只是能設(shè)置多個(gè)動(dòng)畫路徑 使用方法也類似, 大致為

  1. 創(chuàng)建原始UI或者畫面
  2. 創(chuàng)建CAKeyframeAnimation實(shí)例, 并設(shè)置keypart/duration/values 相比基本動(dòng)畫只能設(shè)置開(kāi)始和結(jié)束點(diǎn), 關(guān)鍵幀動(dòng)畫能添加多個(gè)動(dòng)畫路徑點(diǎn)
  3. 設(shè)置動(dòng)畫最終停留的位置
  4. 將配置好的動(dòng)畫添加到layer層中
    舉個(gè)例子, 紅色圓形左右晃動(dòng)往下墜落 上代碼:
//設(shè)置原始畫面
    UIView *showView               = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    showView.layer.masksToBounds   = YES;
    showView.layer.cornerRadius    = 50.f;
    showView.layer.backgroundColor = [UIColor redColor].CGColor;
    
    [self.view addSubview:showView];
    
    //創(chuàng)建關(guān)鍵幀動(dòng)畫
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
    
    //設(shè)置動(dòng)畫屬性
    keyFrameAnimation.keyPath              = @"position";
    keyFrameAnimation.duration             = 4.0f;
    
    keyFrameAnimation.values = @[[NSValue valueWithCGPoint:showView.center],
                                 [NSValue valueWithCGPoint:CGPointMake(100, 100)],
                                 [NSValue valueWithCGPoint:CGPointMake(50, 150)],
                                 [NSValue valueWithCGPoint:CGPointMake(200, 200)]];
    
    //設(shè)置動(dòng)畫結(jié)束位置
    showView.center = CGPointMake(200, 200);
    
    //添加動(dòng)畫到layer層
    [showView.layer addAnimation:keyFrameAnimation forKey:nil];

最后是利用緩動(dòng)函數(shù)配合關(guān)鍵幀動(dòng)畫實(shí)現(xiàn)比較復(fù)雜的物理性動(dòng)畫
先說(shuō)說(shuō)什么是緩動(dòng)函數(shù), 就是有高人寫了一個(gè)庫(kù)可以計(jì)算出模擬物理性動(dòng)畫(比如彈簧效果)所要的路徑
Github地址: https://github.com/YouXianMing/EasingAnimation
具體有哪些動(dòng)畫效果可看庫(kù)中的緩動(dòng)函數(shù)查詢表, 簡(jiǎn)單舉個(gè)小球落地的效果
上代碼:

//設(shè)置原始畫面
    UIView *showView               = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    showView.layer.masksToBounds   = YES;
    showView.layer.cornerRadius    = 50.f;
    showView.layer.backgroundColor = [UIColor redColor].CGColor;
    
    [self.view addSubview:showView];
    
    //創(chuàng)建關(guān)鍵幀動(dòng)畫
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
    
    //設(shè)置動(dòng)畫屬性
    keyFrameAnimation.keyPath              = @"position";
    keyFrameAnimation.duration             = 4.0f;
    
    //關(guān)鍵處, 在這里使用的緩動(dòng)函數(shù)計(jì)算點(diǎn)路徑
    keyFrameAnimation.values = [YXEasing calculateFrameFromPoint:showView.center
                                                         toPoint:CGPointMake(50, 300)
                                                            func:BounceEaseOut
                                                      frameCount:4.0f * 30];
    
    //設(shè)置動(dòng)畫結(jié)束位置
    showView.center = CGPointMake(50, 300);
    
    //添加動(dòng)畫到layer層
    [showView.layer addAnimation:keyFrameAnimation forKey:nil];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,261評(píng)論 5 13
  • IOS動(dòng)畫+轉(zhuǎn)場(chǎng)動(dòng)畫 #import "ViewController.h" #import "secondView...
    iOS小開(kāi)發(fā)閱讀 951評(píng)論 0 1
  • 在iOS實(shí)際開(kāi)發(fā)中常用的動(dòng)畫無(wú)非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,313評(píng)論 1 23
  • 先看看CAAnimation動(dòng)畫的繼承結(jié)構(gòu) CAAnimation{ CAPropertyAnimation { ...
    時(shí)間不會(huì)倒著走閱讀 1,795評(píng)論 0 1

友情鏈接更多精彩內(nèi)容