iOS关键帧图像奇异旋转

iOS关键帧图像奇异旋转,ios,cakeyframeanimation,Ios,Cakeyframeanimation,我正面临着一个奇怪的问题 代码如下: //Inside the selector method of the button CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [anim setDelegate:self]; anim.path = racetrack.path; anim.rotationMode = kCAAnimationRotateAuto; anim.re

我正面临着一个奇怪的问题

代码如下:

//Inside the selector method of the button
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[anim setDelegate:self];
anim.path = racetrack.path;
anim.rotationMode = kCAAnimationRotateAuto;
anim.repeatCount = 0;
anim.duration = 10.0;

[carBtn.layer addAnimation:anim forKey:@"race"];
动画效果很好。 按下carBtn(带有汽车背景图像的按钮)时会出现问题。 按钮的图像毫无理由地旋转了180°(至少我没有理由)。 在创建动画之后,我必须用这行代码自己旋转按钮

carBtn.transform = CGAffineTransformMakeRotation(M_PI);
…但在一到两秒钟内(动画开始之前),汽车的朝向与其初始位置相反。按钮的创建方式如下:

UIImage* car = [UIImage imageNamed:@"car_image"];
carBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[carBtn setFrame:CGRectMake(startPoint.x-45, startPoint.y-33, macchina.size.width, macchina.size.height)];
[carBtn setBackgroundImage:car forState:UIControlStateNormal];
[carBtn setBackgroundImage:car forState:UIControlStateHighlighted];
[carBtn addTarget:self action:@selector(carPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:carBtn];
这怎么可能呢?
谢谢

这就是它的工作原理:视图的X轴沿路径的切线对齐


如果你应用的旋转方式对你有效,我会坚持下去。否则,您将找到一个完整的解决方案。

谢谢您的帮助。不幸的是,在我的情况下,您链接的解决方案不起作用。我尝试使用Caleb的“动画”方法,但UIView animationWithDuration块的持续时间不起作用。虽然我已经将持续时间设置为10秒,但汽车在不到1秒的时间内沿着路径行驶。关于这个问题有什么建议吗?我想问题是我沿着负x坐标的路径方向。因此,我创建了反向路径,第一次沿着正x坐标移动。但问题依然存在。按钮(或图像)在动画开始前旋转180°。令人沮丧的!