Replicate Camera App Rotation to IOS 6 iPhone Album

Hi, I am trying to reproduce the same rotation that can be seen in the camera application when the orientation is shifted to landscape. Unfortunately, I’m out of luck. I need to configure this for a custom CameraOverlayView with a UIImagePickerController.

From this portrait (B - UIButtons)

|-----------| | | | | | | | | | | | | | BBB | |-----------| 

To this landscape

 |----------------| | B | | | | B | | | | B | |----------------| 

In other words, I would like the buttons to stick to the original bottom of the portrait and rotate around their centers. I use Storiesboards and Autolayout is included. Any help is appreciated.

+7
source share
1 answer

OK, so I managed to figure it out. It should be noted that the UIImagePickerController class only supports portrait mode in accordance with the Apple documentation .

To capture rotation willRotateToInterfaceOrientation is useless here, so you need to use notificatons. In addition, setting autodetection restrictions at run time is not appropriate.

In AppDelegate didFinishLaunchingWithOptions you need to enable rotation notifications:

 // send notification on rotation [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications]; 

In the viewDidLoad method of the OverlayView UIViewController add the following:

 //add observer for the rotation notification [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

Finally, add the orientationChanged: method to the camera UIViewController

 - (void)orientationChanged:(NSNotification *)notification { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; double rotation = 0; switch (orientation) { case UIDeviceOrientationPortrait: rotation = 0; break; case UIDeviceOrientationPortraitUpsideDown: rotation = M_PI; break; case UIDeviceOrientationLandscapeLeft: rotation = M_PI_2; break; case UIDeviceOrientationLandscapeRight: rotation = -M_PI_2; break; case UIDeviceOrientationFaceDown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationUnknown: default: return; } CGAffineTransform transform = CGAffineTransformMakeRotation(rotation); [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ self.btnCancel.transform = transform; self.btnSnap.transform = transform; }completion:nil]; } 

The above code applies the rotation conversion to 2 UIButtons, which I use btnCancel and btnSnap in this case. This gives you the effect of the Camera app while rotating the device. I still get a warning in the console <Error>: CGAffineTransformInvert: singular matrix. I don’t know why this is happening, but it has something to do with the camera view.

Hope this helps.

+15
source

All Articles