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];