Question

My iOS has registered for device orientation change notifications via NSNotificationCenter, and has a method that responds to the different orientation changes. The following code is called to change the orientation and location of a view when rotating the phone to "landscape-left".

camAngle = [(NSNumber *)[_cameraToggleButton valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
            camDegrees = (RADIANS_TO_DEGREES(camAngle));
            if (!(camDegrees == 90)) {
                _cameraToggleButton.alpha = 0.0;
                _cameraToggleButton.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
                _cameraToggleButton.frame = CGRectMake(274, 350, 66, 54);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.4];
                [UIView setAnimationCurve:UIViewAnimationCurveLinear];
                _cameraToggleButton.alpha = 1.0;
                [UIView commitAnimations];
            }

And my equations to convert radians and degrees:

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

The view rotates correctly and assumes the correct position on screen, but the sides of the image are suddenly squashed, making the image look condensed. This only happens when rotating the phone into landscape position (left or right). Rotating to upside down or back to portrait translates and rotates the view fine, with essentially the same code. What could cause this skewing of the images in landscape position?

Was it helpful?

Solution

You should not set the frame of the _cameraToggleButton after you set the rotation transform on it. To re-position the _cameraToggleButton after you set the rotation, you should change it's center like so:

_cameraToggleButton.center = CGPointMake(274, 350);

Also, as someone else suggested, use UIViewContentModeScaleAspectFit instead of UIViewContentModeScaleToFill.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top