繁体   English   中英

方向变化

[英]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.

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