Search code examples
objective-cios8screen-rotation

Is it possible to detect that an iPhone has rotated without the orientation of the view changing?


In a nutshell, I want to manually rotate only a few elements on the screen when the device goes landscape but keep the actual app in portrait mode. How can I hinge on this event in the background?


Solution

  • Have you tried using

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    

    This only caters for device orientation, so if you have other orientations disabled. It still gives correct value for which ever orientation your device is in. You can compare its values to (and more, you can view those at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/index.html#//apple_ref/c/tdef/UIDeviceOrientation)

    UIDeviceOrientationPortrait,
    UIDeviceOrientationPortraitUpsideDown,
    UIDeviceOrientationLandscapeRight,
    UIDeviceOrientationLandscapeLeft
    

    I Hope this helps.

    Edit :

    For an event you have to add an observer to detect the device's orientation being changed. Register the notification/observer in appdelegate or where ever u feel it is needed and place orientationChanged method in the class where u delegated the observer. (probably in the same class in most scenarios).

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter]
         addObserver:self selector:@selector(orientationChanged:)
         name:UIDeviceOrientationDidChangeNotification
         object:[UIDevice currentDevice]];
    
    - (void) orientationChanged:(NSNotification *)note
    {
        NSLog(@"orientationChanged");
        UIDevice * device = note.object;
        if(device.orientation == UIDeviceOrientationPortrait)
        {
    
        }
        // add other else if here
        else
        {
    
        }
    }
    

    Note that orientationChanged is called when you add the observer. Only once. After every time your device changes orientation. Do let me know if you find something confusing.