使用CABasicAnimation的负角度值

时间:2014-04-05 19:04:34

标签: ios objective-c rotation transform cabasicanimation

我使用以下代码使用UIImageView连续转动CABasicAnimation

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:M_PI * 2];
fullRotation.duration = 6;
fullRotation.repeatCount = HUGE_VAL;
[self.myImageView.layer addAnimation:fullRotation forKey:@"360"];

动画按预期工作,但当我记录旋转角度时,它会从0变为180,然后从-180变为0。我希望它从0转到360然后再回到0

我使用以下代码获取旋转角度:

CGFloat radians = [[self.myImageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue];

CGFloat degrees = (radians * (180 / M_PI));

NSLog(@"ROTATION = %f", radians);

1 个答案:

答案 0 :(得分:0)

旋转角度唯一确定的最大值为360度(或2 *π弧度)的倍数,例如-90度和270度描述完全相同的旋转。

[[self.myImageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue]

给出图像视图在特定时间点和旋转时的旋转 可以使用0到2 *π之间的角度(如您所料)或使用角度来描述 在-π和π之间(实际发生的是)。

将任意角度标准化为范围0 <= degrees < 360 你可以做到

degrees = fmod(degrees, 360.0);
if (degrees < 0) degrees += 360.0;