UIRotationGestureRecognizer 旋转有时会达到非常大的值

发布于 2024-12-18 07:33:55 字数 704 浏览 2 评论 0原文

我正在使用 UIRotationGestureRecognizer 来旋转 UIImageView。通过将所有小的旋转变化添加到 CGFloat 中,我尝试计算 UIImageView 的总旋转量。它确实很好,但不知何故,手势的旋转属性有时具有非常大的值。通常,当缓慢旋转时,它位于 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);
}

I'm using a UIRotationGestureRecognizer to rotate a UIImageView. By adding all small rotation changes to a CGFloat, I try to calculate the total rotation of the UIImageView. It does fine, but somehow the rotation property of the gesture sometimes has a very large value. Normally, when rotating slow, it sits around 0.00##, but then it suddenly starts giving values like 6.##. The end result is a total of > 300 radians, which is ridiculous - over 47 'revolutions' for just a millimeter worth of finger movement.

Does anyone know what causes this, and more importantly, have a solution to it?

Here's some code:

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-12-25 07:33:56

我也明白了。 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,所以这对我来说是个问题。

I'm getting this too. The 6.## values are around 2 * PI (ie 360 degrees). Can be +/- 6.## in my experience. I've even got +/- 12.## (4 * PI) Rather than spending time working out why, I'm just checking and correcting:

CGFloat useRotation = gesture.rotation;

while( useRotation < -M_PI )
    useRotation += M_PI*2;

while( useRotation > M_PI )
    useRotation -= M_PI*2;

Actually you shouldn't need to do this because you are using the angle directly. Sin(angle +/- PI*2) = Sin(angle) etc. Don't worry about the 300 radians, it will still get the transform right. But I'm dividing the angle by 2 so this was a problem for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文