UIInterfaceOrientationMask is defined as:
typedef enum {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape =
(UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll =
(UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown =
(UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight),
} UIInterfaceOrientationMask;
为了一个简单的工作,让我们简化枚举:
typedef enum {
UIInterfaceOrientationMaskPortrait = (1 << 0),
UIInterfaceOrientationMaskLandscapeLeft = (1 << 1),
UIInterfaceOrientationMaskLandscapeRight = (1 << 2),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << 3)
} UIInterfaceOrientationMask;
这意味着:
typedef enum {
UIInterfaceOrientationMaskPortrait = 0001,
UIInterfaceOrientationMaskLandscapeLeft = 0010,
UIInterfaceOrientationMaskLandscapeRight = 0100,
UIInterfaceOrientationMaskPortraitUpsideDown = 1000
} UIInterfaceOrientationMask;
这是可能的,因为此枚举使用 C 位移位: http ://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
然后当我们写:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait | UInterfaceOrientationMaskLandscapeLeft;
}
事实上我们正在返回:0011
为什么?因为二进制 OR
0001 或 0010 = 0011
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
到目前为止,我明白了。
但是,该方法如何检查哪个方向有效?
因为如果我们有一个简单的枚举,我们正在检查 is 是否等于 0、1、2 或 3
typedef enum {
simpleZero,
simpleOne ,
simpleTwo ,
simpleThree
} simple;
int whatever = someNumber
if (whatever == simpleZero)
{
}
else if (whatever == simpleOne)
{
}
.......
但是,代码是如何处理 UIInterfaceOrientationMask 的?使用二进制与?
if (returnFromSupportedInterfaceOrientations & UIInterfaceOrientationMaskPortrait == UIInterfaceOrientationMaskPortrait)
{
// Do something
// Here is TRUE 0011 AND 0001 = 0001
}
if (returnFromSupportedInterfaceOrientations & UIInterfaceOrientationMaskLandscapeLeft == UIInterfaceOrientationMaskLandscapeLeft)
{
// Do something
// Here is TRUE 0011 AND 0010 = 0010
}
if (returnFromSupportedInterfaceOrientations & UIInterfaceOrientationMaskLandscapeLeft == UIInterfaceOrientationMaskLandscapeLeft)
{
// Do something
// Here is FALSE 0011 AND 0100 = 0000
}
是这样吗?
谢谢