方向改变

时间:2011-11-14 12:39:29

标签: iphone ios uiinterfaceorientation

在iOS中使用以下两种方法进行方向更改时有什么不同。

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方向委托方法

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

1 个答案:

答案 0 :(得分:1)

在第一种情况下,您将收到UIDeviceOrientation。[[UIDevice currentDevice] orientation]返回 UIDeviceOrientation ,设备方向。但请注意,UIDeviceOrientation并不总是UIInterfaceOrientation。例如,当您的设备位于普通表上时,您可以收到意外值(UIDeviceOrientationUnknown)。

shouldAutorotateToInterfaceOrientation方法中,您可以访问界面的当前方向UIInterfaceOrientation。

相关问题