UIInterfaceOrientationMaskPortrait和UIInterfaceOrientationPortrait有什么区别

Ste*_*ris 8 objective-c ios xcode4

我看过文档,但无法找到为什么有时会插入掩码这个词,有时候不会.

ric*_*h.e 5

UIInterfaceOrientationMask iOS 6使用

-[UIViewController supportedInterfaceOrientations]
Run Code Online (Sandbox Code Playgroud)

方法(此处docs),它是视图控制器将采用的所有方向的位掩码,由sdk调用.以下是一个示例实现:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape; // supports both landscape modes
}
Run Code Online (Sandbox Code Playgroud)

这与之前的iOS 6(现已弃用,此处为docs)方法形成对比:

-[UIViewController shouldAutorotateToInterfaceOrientation:]
Run Code Online (Sandbox Code Playgroud)

它为你提供了一个UIInterfaceOrientation枚举值,你用以下的方法测试了它:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
   return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)

请注意,您仍然可以获得UIInterfaceOrientationdidRotate方法中的枚举以及interfaceOrientaiton属性,这在其他时间可能很有用.掩码仅在sdk决定是否旋转视图控制器时使用.

别人的问题:有人注意到没有UIInterfaceOrientationMaskPortraitAll面具吗?似乎对我不了解.

  • @ rich.e UIInterfaceOrientationMaskPortraitAll不是必需的,因为UIInterfaceOrientationMaskPortrait =(1 << UIInterfaceOrientationPortrait)并且在enum中对应于索引号0的UIDeviceOrientationPortrait的位置. (2认同)