简体   繁体   中英

Orientation change

is there any difference when we use following two methods for orientation change in iOS.

1) Using NotificationCenter

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];

-(void) orientationChange
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSLog(@"Orientation =%d",orientation);
    NSLog(@"in Testtt");
}

2) ViewController Orientation delegate methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if(UIInterfaceOrientationLandscapeRight == interfaceOrientation || UIInterfaceOrientationLandscapeLeft == interfaceOrientation)
        return YES;
    else 
        return NO;
}

In first case you will receive UIDeviceOrientation. [[UIDevice currentDevice] orientation] returns UIDeviceOrientation , device orientation. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value ( UIDeviceOrientationUnknown ).

In shouldAutorotateToInterfaceOrientation method you can access UIInterfaceOrientation, current orientation of the interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM