Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling auto rotation for a UIView

Tags:

My application is composed of a toolbar and an AVCaptureVideoPreviewLayer in a background UIView. I'd like to see that toolbar rotate regarding the device orientation, so in my main ViewController, I implemented the function :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait
            || interfaceOrientation == UIInterfaceOrientationLandscapeLeft
            || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

This works fine, but when the interface orientation is changed, I see that the background UIView (with a video layer) is also rotating, so my video view is now playing 90 degrees left or right... Which is not really cool !
So my question is : is there any way to disable the auto-rotation animation on a specific UIView ?

I have seen one similar question to this one : Disabling autorotate for a single UIView, but the answers doesn't fit my problem, I really need the background view to do not move, not to get around the problem with a kind of "counter animation". So I decided to bring up this topic.

Any ideas ?

Thanks !

like image 546
Thomas Desert Avatar asked Jul 21 '10 16:07

Thomas Desert


People also ask

What happens when the device orientation of a view controller changes?

When the device orientation changes, the system calls this method on the root view controller or the topmost modal view controller that fills the window. If the view controller supports the new orientation, the system rotates the window and the view controller.

Should I enable or disable the home button orientation on iOS?

It’s best practice to enable it for the iPad idiom. iOS devices without a Home button, such as iPhone 12, don’t support this orientation. You should disable it entirely for the iPhone idiom.

Should I enable or disable portrait upside down on my iPad?

All iPadOS devices support the portraitUpsideDown orientation. It’s best practice to enable it for the iPad idiom. iOS devices without a Home button, such as iPhone 12, don’t support this orientation. You should disable it entirely for the iPhone idiom.


1 Answers

This post pointed in the right direction. To get an AVCaptureVideoPreviewLayer to not rotate with the device I wrapped it in a UIView and animated that view in the opposite direction:

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    float rotation;

    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
        rotation = 0;
    } else
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        rotation = M_PI/2;
    } else
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
        rotation = -M_PI/2;
    }

    [UIView animateWithDuration:duration animations:^{
        cameraPreview.transform = CGAffineTransformMakeRotation(rotation);
        cameraPreview.frame = self.view.frame;
    }];
}

It seems a little roundabout but it animates smoothly. Other views on top of the preview layer automatically rotate as they should.

like image 126
Anthony Mattox Avatar answered Sep 19 '22 02:09

Anthony Mattox