2011-11-14 149 views
0

当我们在iOS中使用以下两种方法更改方向时,是否有任何区别。方向更改

1)使用通知中心

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

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

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