Dummy Code
Dummy Code

Reputation: 1898

Rotate and Scale with auto-layout in iOS6

This is an issue I have been having for a good while now. With the new iOS6 and auto-layout my gesture recognizer "game" is no longer working.

I have created an app using storyboards and on one of the tabs there is a little game application where the user must scale, rotate, and pan an image on top of another image within 5 pixels, 5% size, and 5 degrees. The issue I am having with auto-layout is that when rotate and scaling the game image the image "jumps" back to it's starting position and rotates awkwardly and not around the center.

My methods for handling the rotate and scale gestures are below.

Rotate

//rotation gesture recognizer response
- (void)respondToRotateGesture:(UIRotationGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        image.transform = CGAffineTransformRotate(gesture.view.transform, gesture.rotation);
    }
    if (gesture.state == UIGestureRecognizerStateEnded) [self didWin]; //not important for question
    gesture.rotation = 0;
}

Pinch

//pinch gesture recognizer
- (void)respondToPinchGesture:(UIPinchGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
            image.transform = CGAffineTransformScale(image.transform, gesture.scale, gesture.scale);
    }
    if (gesture.state == UIGestureRecognizerStateEnded) [self didWin]; //not important for question
    gesture.scale = 1;
}

I do realize that the easiest solution would be to turn off auto-layout but that causes issues in the rest of the storyboard because in storyboards you cannot turn of auto-layout for one component but you must turn it off for the entire storyboard.

Can anyone advise me what to do? I have read that this is a major issue and some are calling it a "bug" with the new auto-layout.

I am just having problems with this and I need some help

If anyone has any suggestions or can point me in the right direction it would be much appreciated. Thanks!

-Henry

Upvotes: 1

Views: 1804

Answers (1)

Dummy Code
Dummy Code

Reputation: 1898

The easiest way around this was to create the image programmatically using the following code to remove auto-layout. Since you cannot set image.translatesAutoresizingMaskIntoConstraints = YES if you add the image into the view using IB due to the apple "bug" with auto-layout. Because you will get an error saying, "Unable to simultaneously satisfy constraints"

So you need to do this programmatically because IB adds constraints and you don't want any.

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    image.image = [UIImage imageNamed:@"gorilla_red.png"];
    image.translatesAutoresizingMaskIntoConstraints = YES;
    [self.view addSubview:image];

Upvotes: 3

Related Questions