iOS之高級動畫的CAKeyframeAnimation 關鍵幀動畫

1 實現左右搖動動畫

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];

 CGFloat angel = M_PI_4 / 12.0;

 [anim setValues:@[@(angel), @(-angel), @(angel)]];

[anim setRepeatCount:HUGE_VALF];

 [self.anima.layer addAnimation:anim forKey:nil];

2 實現貝塞爾曲線的動畫

  // 1. 實例化關鍵幀動畫
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    // 2. 設置路徑
    [anim setDuration:duration];
    
    // 中間的控制點使用屏幕上得隨機點
    CGPoint cp = [self randomPoint];
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    // 設置起始點
    CGPathMoveToPoint(path, NULL, self.center.x, self.center.y);
    // 添加帶一個控制點的貝塞爾曲線
    CGPathAddQuadCurveToPoint(path, NULL, cp.x, cp.y, to.x, to.y);
    
    [anim setPath:path];
    CGPathRelease(path);
    
    // 5) 設置鍵值記錄目標位置,以便動畫結束後,修正位置
    [anim setValue:@"translationTo" forKey:@"animationType"];
    [anim setValue:[NSValue valueWithCGPoint:to] forKey:@"targetPoint"];
    [anim setDelegate:self];
    
    // 3. 將動畫添加到圖層
    [self.layer addAnimation:anim forKey:nil];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章