RotationMode在CAKeyframeAnimation(线性路径)中不起作用

时间:2012-12-04 09:07:07

标签: ios core-animation cakeyframeanimation

我尝试使视图旋转只要它通过路径(线性路径)但它不会旋转。

这是我的代码:

 CAKeyframeAnimation *animKF = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animKF.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 100)], [NSValue valueWithCGPoint:CGPointMake(400, 400)], [NSValue valueWithCGPoint:CGPointMake(700, 100)], [NSValue valueWithCGPoint:CGPointMake(1000, 400)], nil];

animKF.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.25], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil];

animKF.duration = 6;

animKF.rotationMode =  kCAAnimationRotateAuto;

[imageView.layer addAnimation:animKF forKey:@"keyframe"];

但是imageView(图片中的蓝色面)不会沿着路径旋转(图片中的红线)

Path

非常感谢你。

1 个答案:

答案 0 :(得分:2)

来自rotationMode的文档:

  

未提供路径对象时将此属性设置为非nil值的效果未定义。

在您的代码中,您要设置关键帧动画的values - 属性,但您需要设置path


您应该根据值创建一个CGPath,并将其设置为关键帧的路径。

UIBezierPath *animationPath = [UIBezierPath bezierPath];
[animationPath moveToPoint:CGPointMake(100, 100)];
[animationPath addLineToPoint:CGPointMake(400, 400)];
[animationPath addLineToPoint:CGPointMake(700, 100)];
[animationPath addLineToPoint:CGPointMake(1000, 400)];

animKF.path = animationPath.CGPath;