Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
The rotation gesture works. The image rotates fine. But I'm trying to find out how much the image rotates by recording some sort of linear change in some value. I'm failing. Can anyone help?

The NSLog statements below generate random numbers that I can't explain. Am I on the right track here? Has anyone found a solution to this?

Code:
-(void)rotationGestureRecognized:(UIRotationGestureRecognizer*)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        lastRotation = recognizer.rotation;
    }
    
    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGFloat newRotation = 1 - (lastRotation - recognizer.rotation);
        if (newRotation < 1)
        {
            //Clockwise
            CGFloat rotation = 1 - newRotation;
            totalClockwiseRotation += rotation;
            totalAntiClockwiseRotation += rotation;
        } else if (newRotation >= 1)
        {
            //Anti-Clockwise
            CGFloat rotation = newRotation - 1;
            totalAntiClockwiseRotation -= rotation;
            totalClockwiseRotation -= rotation;
        }
        //Perform Rotation
        CGAffineTransform transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
        recognizer.view.transform = transform;
        recognizer.rotation = 0;
    }
    
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        if (totalAntiClockwiseRotation > 0) totalClockwiseRotation = 0;
        if (totalClockwiseRotation < 0) totalAntiClockwiseRotation = 0;
        
        NSLog(@"totalAntiClockwiseRotation: %f", totalAntiClockwiseRotation);
        NSLog(@"totalClockwiseRotation: %f", totalClockwiseRotation);
    }
}
 
Last edited:
Solved it with one line of code

Code:
CGFloat currentRotation = [[recognizer.view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];

AAAAAAAAAAAAAAAHHHHHHHHHHHHH!!!!!!!!!
 
Solved it with one line of code

Code:
CGFloat currentRotation = [[recognizer.view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];

AAAAAAAAAAAAAAAHHHHHHHHHHHHH!!!!!!!!!

I knew you could set a layer's rotation using the "transform.rotation.z" key, but I always assumed that was read-only. Interesting to know that you can use it to ask the rotation of a layer's transform as well.

Extracting the rotation from a transformation matrix is fairly hard work, mathematically speaking.

You would be better off by setting up a rotation instance variable, and using that to keep track of/set the rotation of your layer.

Each time you get the rotation value from your gesture recognizer, add it to your rotation iVar.

Then use that rotation iVar to create a transformation matrix rather than adding the rotation to the existing matrix.
 
How do you mark a thread as solved?

Solution:
Code:
-(void)rotationGestureRecognized:(UIRotationGestureRecognizer*)recognizer
{
    //Clockwise
    //0 to M_PI
    
    //AntiClockwise
    //0 to -M_PI
    
    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {                
        //Perform Rotation
        CGAffineTransform transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
        recognizer.view.transform = transform;
        recognizer.rotation = 0;
    }
    
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        CGFloat currentRotation = [[recognizer.view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
        
        const CGFloat North = 0;
        const CGFloat East = M_PI/2;
        const CGFloat West = -M_PI/2;
        const CGFloat SouthPositive = M_PI;
        const CGFloat SouthNegative = -M_PI;
        
        const CGFloat NNE = M_PI/4;
        const CGFloat NNW = -M_PI/4;
        const CGFloat SSE = M_PI - (M_PI/4);
        const CGFloat SSW = -M_PI + (M_PI/4);
        
        if (currentRotation >= SouthNegative && currentRotation <= West) {
            if (currentRotation >= SSW) {
                NSLog(@"A");
            } else {
                NSLog(@"B");
            }
        } else if (currentRotation >= West && currentRotation < North) {
            if (currentRotation >= NNW) {
                NSLog(@"C");
            } else {
                NSLog(@"D");
            }
        } else if (currentRotation == North) {
            NSLog(@"E");
        } else if (currentRotation > North && currentRotation <= East) {
            if (currentRotation <= NNE) {
                NSLog(@"F");
            } else {
                NSLog(@"G");
            }
        } else if (currentRotation >= East && currentRotation <= SouthPositive) {
            if (currentRotation <= SSE) {
                NSLog(@"H");
            } else {
                NSLog(@"I");
            }
        }
    }
}
 
How do you mark a thread as solved?

Press edit in your first post, then choose advance or full editor or whatever it's called, then there's a Dropdown menu next to the topic. Pick "Resolved" from the Dropdown menu.
 
Press edit in your first post, then choose advance or full editor or whatever it's called, then there's a Dropdown menu next to the topic. Pick "Resolved" from the Dropdown menu.

thanks

definitely not easy to find without instruction!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.