[英]Orientation change
當我們在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;
}
在第一種情況下,您將收到UIDeviceOrientation。 [[UIDevice currentDevice] orientation]
返回UIDeviceOrientation ,設備方向。 但是請注意,UIDeviceOrientation並不總是UIInterfaceOrientation。 例如,當您的設備位於普通表上時,您會收到意外的值( UIDeviceOrientationUnknown
)。
在shouldAutorotateToInterfaceOrientation
方法中,您可以訪問UIInterfaceOrientation(接口的當前方向)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.