Reinder de Vries
Reinder de Vries

Reputation: 522

UIRotationGestureRecognizer rotation sometimes bumps to very large value

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

Upvotes: 4

Views: 1539

Answers (1)

Rogers
Rogers

Reputation: 1973

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.

Upvotes: 2

Related Questions