我正在使用 UIRotationGestureRecognizer
来旋转 UIImageView
。通过将所有小的旋转更改添加到 CGFloat
,我尝试计算 UIImageView
的总旋转。它确实很好,但不知何故,手势的 rotation
属性有时具有非常大的值。通常,当旋转缓慢时,它位于 0.00## 左右,但随后它突然开始给出类似 6.## 的值。最终结果是总共 > 300 弧度,这太荒谬了 - 手指移动一毫米就超过 47 次“旋转”。
有谁知道造成这种情况的原因,更重要的是,有解决方案吗?
这是一些代码:
if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged)
{
totalRotation += [gesture rotation];
NSLog(@"%f", [gesture rotation]);
[gesture view].transform = CGAffineTransformRotate([[gesture view] transform], [gesture rotation]);
[gesture setRotation:0];
}
else
{
NSLog(@"rot: %f", totalRotation);
}
最佳答案
我也明白了。 6.## 值约为 2 * PI(即 360 度)。根据我的经验,可以是 +/- 6.##。我什至得到了 +/- 12.## (4 * PI) 而不是花时间找出原因,我只是检查和纠正:
CGFloat useRotation = gesture.rotation;
while( useRotation < -M_PI )
useRotation += M_PI*2;
while( useRotation > M_PI )
useRotation -= M_PI*2;
实际上您不需要这样做,因为您直接使用了角度。 Sin(angle +/- PI*2) = Sin(angle) 等。不用担心 300 弧度,它仍然会正确转换。但我将角度除以 2,所以这对我来说是个问题。
关于objective-c - UIRotationGestureRecognizer 旋转有时会达到非常大的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8298224/