0

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

这是我的代码:

 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(图中蓝脸)并没有沿路​​径旋转(图中红线)

小路

太感谢了。

4

1 回答 1

2

从以下文档rotationMode

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

在您的代码中,您正在设置values关键帧动画的 -property 但您需要设置 a 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;
于 2012-12-04T09:21:56.017 回答