Core Animation<2>

時(shí)間函數(shù):動(dòng)畫(huà)總會(huì)在設(shè)定的時(shí)間長(zhǎng)度內(nèi)執(zhí)行完,在這段時(shí)間內(nèi),動(dòng)畫(huà)的變化速度有多快,主要決定于時(shí)間函數(shù)

CA_EXTERN NSString * const kCAMediaTimingFunctionLinear  //線性函數(shù)
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn  //緩入
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut  //緩出
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut  //慢入慢出
    CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault  //默認(rèn)
    CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
  • 線性函數(shù)
Paste_Image.png
  • 緩入
Paste_Image.png
  • 緩出
Paste_Image.png
  • 慢入慢出
Paste_Image.png
  • 默認(rèn)

Paste_Image.png

系統(tǒng)提供的設(shè)置時(shí)間函數(shù)的方法[CAMediaTimingFunction functionWithControlPoints::::]里面的四個(gè)參數(shù)范圍值都是0.0 -1.0之間,這些數(shù)值的由來(lái)可以看曲線圖起始點(diǎn)和終點(diǎn)對(duì)應(yīng)的切線。

基本動(dòng)畫(huà).gif

思路:拆分動(dòng)畫(huà)

  • 1.把基礎(chǔ)圖形設(shè)置為兩個(gè)連著的半圓弧
Paste_Image.png
   CAShapeLayer * first = [CAShapeLayer layer];
    first.path = [self wormRunLongPath];
    first.lineWidth = 10;
    first.lineCap = kCALineCapRound;
    first.lineJoin = kCALineJoinRound;
    first.strokeColor = [UIColor redColor].CGColor;
    first.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:first];

- (CGPathRef)wormRunLongPath {
    //中間點(diǎn)
    CGPoint center = CGPointMake(220, 300/2);
    CGFloat radius = 30;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(center.x  - radius , center.y) radius:radius startAngle:M_PI endAngle:2 * M_PI clockwise:YES];
   [path addArcWithCenter:CGPointMake(center.x  + radius, center.y) radius:radius startAngle:M_PI endAngle:2 * M_PI clockwise:YES];
    return path.CGPath;
}

運(yùn)行,就得到了上面兩個(gè)半圓的效果。

  • 2 那么接下來(lái)我們要實(shí)現(xiàn)讓它動(dòng)起來(lái):實(shí)現(xiàn)紅色蟲(chóng)運(yùn)動(dòng)的前半部分如下圖
基本動(dòng)畫(huà)分解1.gif
   //第一階段蟲(chóng)子的拉伸
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:0.5];
   //從0到0.5表示動(dòng)畫(huà)的過(guò)程從無(wú)到一個(gè)半圓
    animation.duration = 0.75;
    animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.0 :1.0 :0.55];
    animation.removedOnCompletion = false;
    animation.fillMode = kCAFillModeForwards;

運(yùn)行,OK.

  • 3 實(shí)現(xiàn)紅色蟲(chóng)前半部收縮的效果如下圖
基本動(dòng)畫(huà)分解2.gif
    //第一階段蟲(chóng)子的縮小
    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    animation1.fromValue = [NSNumber numberWithFloat:0.0];
    animation1.toValue = [NSNumber numberWithFloat:0.5];
    animation1.duration = 0.45;
    animation1.beginTime = 0.75;
    animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation1.fillMode = kCAFillModeForwards;

稍微注意一下我們就發(fā)現(xiàn)其實(shí)只是keyPath的區(qū)別,一個(gè)是start,一個(gè)是end,同事我們?cè)O(shè)置beginTime表示的是在2的效果完成之后才開(kāi)始3的效果,剛好銜接上去就湊成了一個(gè)完整的動(dòng)畫(huà)。然后把這兩個(gè)放到一個(gè)動(dòng)畫(huà)組里面試試效果.

  • 4 實(shí)現(xiàn)前半部分動(dòng)畫(huà)完整銜接如下圖
基本動(dòng)畫(huà)分解3.gif
 CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray  arrayWithObjects:animation,animation1,nil];
    group.repeatCount = HUGE_VALF;
    group.duration = 0.75 * 2;
    [self.firstWormShapeLayer addAnimation:group forKey:@"one"];
  • 5.同理實(shí)現(xiàn)紅色蟲(chóng)后半部分的拉伸和收縮如下圖
基本動(dòng)畫(huà)分解4.gif
基本動(dòng)畫(huà)分解5.gif
//第二階段蟲(chóng)子的拉伸
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation2.fromValue = [NSNumber numberWithFloat:0.5];
    animation2.toValue = [NSNumber numberWithFloat:1.0];
    animation2.duration = 0.75;
    animation2.beginTime = 1.2;
    animation2.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.42 :0.0 :1.0 :0.55];
    animation2.fillMode = kCAFillModeForwards;
    //第二階段蟲(chóng)子的縮小
    CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    animation3.fromValue = [NSNumber numberWithFloat:0.5];
    animation3.toValue = [NSNumber numberWithFloat:1];
    animation3.duration = 0.45;
    animation3.beginTime = 0.75 + 1.2;
    animation3.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation3.fillMode = kCAFillModeForwards;
  • 6 把上面四個(gè)動(dòng)畫(huà)一起放入動(dòng)畫(huà)組里面實(shí)現(xiàn)效果如下圖


    基本動(dòng)畫(huà)分解6.gif
  • 7 然后分析一下我們發(fā)現(xiàn)跟效果圖就差了一個(gè)關(guān)于x的位移
      //實(shí)現(xiàn)持續(xù)移動(dòng)的效果:第一階段平移
      CABasicAnimation *xTranslationAm = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
      xTranslationAm.toValue = [NSNumber numberWithFloat: (60 / -1.0)];
      xTranslationAm.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
      xTranslationAm.duration = 1.18;
      xTranslationAm.fillMode = kCAFillModeForwards;
      //實(shí)現(xiàn)持續(xù)移動(dòng)的效果:第二階段平移
      CABasicAnimation *xTranslationAm2 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
      xTranslationAm2.toValue = [NSNumber numberWithFloat: (60 / -1.0) * 2];
      xTranslationAm2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
      xTranslationAm2.duration = 1.18;
      xTranslationAm2.beginTime = 1.20;
      xTranslationAm2.fillMode = kCAFillModeForwards;
    
把位移加入到動(dòng)畫(huà)組里面運(yùn)行就實(shí)現(xiàn)了紅色的效果

![基本動(dòng)畫(huà)分解7.gif](http://upload-images.jianshu.io/upload_images/1851080-6b2a75e429e3baf3.gif?imageMogr2/auto-orient/strip)

同理,可實(shí)現(xiàn)黃色和綠色的加入。

最后編輯于
?著作權(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)容

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