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

发布了368 篇原创文章 · 获赞 22 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/BianHuanShiZhe/article/details/104972339