Resolved Gesture rotate record angle

Discussion in 'iPhone/iPad Programming' started by nashyo, Sep 9, 2012.

  1. nashyo, Sep 9, 2012
    Last edited: Sep 9, 2012

    macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    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);
        }
    }
     
  2. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #2
    Solved it with one line of code

    Code:
    CGFloat currentRotation = [[recognizer.view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    AAAAAAAAAAAAAAAHHHHHHHHHHHHH!!!!!!!!!
     
  3. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    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.
     
  4. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #4
    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");
                }
            }
        }
    }
     
  5. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    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.
     
  6. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #6
    thanks

    definitely not easy to find without instruction!
     

Share This Page