开发者

Detect orientation change in a UIImagePickerController?

开发者 https://www.devze.com 2022-12-16 19:55 出处:网络
Im trying to detect orientation changes in a UIImagePickerController (it inherits from UINavigationController : UIViewController : UIResponder : NSObject) and I tried override the method - (BOOL)shoul

Im trying to detect orientation changes in a UIImagePickerController (it inherits from UINavigationController : UIViewController : UIResponder : NSObject) and I tried override the method - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrienta开发者_如何学编程tion in UIViewController but no success...

any tips?

Thanks in advance...


Subclassing UIImagePickerController is not supported!

This class is intended to be used as-is and does not support subclassing.

Maybe you could register for UIDeviceOrientationDidChangeNotification from UIDevice and use this?


This is too late to answer here, but I'm expanding @Adam Woś answer,

Before presenting UIImagePickerController,

UIImagePickerController *controllerObject = [[UIImagePickerController alloc] init];
...
...
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification  object:nil];
...
...
[self presentViewController:controllerObject animated:YES completion:nil];

- (void)orientationChanged:(NSNotification *)notification{
    [self adjustViewsForOrientation:UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
    if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
    {
        NSLog(@".....landscape.....");
    }
    else if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        NSLog(@".....portrait.....");
    }
}

When UIImagePickerController get dismiss don't forget,

[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Also note that, as per @FelixLam comment on @Adam Woś answer,

if device orientation is locked then notifications no longer posted.

To handle this situation, one need to implement CoreMotion (alternative UIAccelerometer iOS < 6.0) to detect that device orientation is locked or not. Here's the blog for UIAccelerometer http://blog.sallarp.com/iphone-accelerometer-device-orientation/ and this is for CoreMotion https://github.com/tastyone/MotionOrientation You'll have to kick some logic to check this.

Good luck!


From the official UIImagePickerController documentation:

Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. In iPhone OS 3.1 and later, you can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消