简体   繁体   English

方向变化

[英]Orientation change

is there any difference when we use following two methods for orientation change in iOS. 当我们在iOS中使用以下两种方法进行方向更改时,有什么区别。

1) Using NotificationCenter 1)使用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 2)ViewController方向委托方法

- (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. 在第一种情况下,您将收到UIDeviceOrientation。 [[UIDevice currentDevice] orientation] returns UIDeviceOrientation , device orientation. [[UIDevice currentDevice] orientation]返回UIDeviceOrientation ,设备方向。 But note that UIDeviceOrientation is not always UIInterfaceOrientation. 但是请注意,UIDeviceOrientation并不总是UIInterfaceOrientation。 For example, when your device is on a plain table you can receive unexpected value ( UIDeviceOrientationUnknown ). 例如,当您的设备位于普通表上时,您会收到意外的值( UIDeviceOrientationUnknown )。

In shouldAutorotateToInterfaceOrientation method you can access UIInterfaceOrientation, current orientation of the interface. shouldAutorotateToInterfaceOrientation方法中,您可以访问UIInterfaceOrientation(接口的当前方向)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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