是否有可能检测到iPhone已旋转而视图方向没有变化?

时间:2014-11-06 02:38:03

标签: objective-c ios8 screen-rotation

简而言之,我想在设备移动时手动旋转屏幕上的几个元素,但保持实际应用程序处于纵向模式。我如何在后台依靠此事件?

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

这仅适用于设备方向,因此如果您禁用其他方向。它仍然为您的设备所处的方向提供正确的值。您可以将其值进行比较(以及更多,您可以在https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/index.html#//apple_ref/c/tdef/UIDeviceOrientation查看这些值)

UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationLandscapeLeft

我希望这有帮助。

编辑:

对于某个事件,您必须添加一个观察者来检测设备的方向是否已更改。在appdelegate中注册通知/观察者或者在需要的地方注册通知/观察者,并将orientationChanged方法放在您委派观察者的类中。 (在大多数情况下可能属于同一类)。

[[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
    {

    }
}

请注意,添加观察者时会调用orientationChanged。只有一次。每次设备更改方向后。如果你发现令人困惑的事情,请告诉我。