我在不断地旋转 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);
最佳答案
一个旋转角度唯一确定的最多只有 360 度(或 2*π 弧度)的倍数,例如 -90 度和 270 度描述完全相同的旋转。
[[self.myImageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue]
给出 ImageView 在特定时间点的旋转,并且该旋转
可以使用 0 到 2*π 之间的角度(如您所料)或使用角度来描述
在 -π 和 π 之间(这是实际发生的情况)。
将任意角度归一化到
0 <= degrees < 360
范围内你可以做
degrees = fmod(degrees, 360.0);
if (degrees < 0) degrees += 360.0;
关于ios - 使用 CABasicAnimation 的负角度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22885417/