通过UIInterfaceOrientationMask for Objective-C / iOS了解C++枚举


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

为什么?因为二进制或

0001 或 0010 = 0011

0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1

到目前为止,我明白了。

但是,该方法如何检查哪个方向有效?

因为如果我们有一个简单的枚举,我们正在检查是否等于 0、1、2 或 3

typedef enum {
   simpleZero,
   simpleOne ,
   simpleTwo ,
   simpleThree 
} simple;
int whatever = someNumber
if (whatever == simpleZero)
{
}
else if  (whatever == simpleOne)
{
}
.......

但是,代码如何处理UIInterfaceOrientationMask?使用二进制 AND?

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
}

是这样吗?

谢谢

/

/...

事实上,我们返回:0011

为什么?

因为一位非常聪明的苹果工程师决定为他的枚举分配位移值。 如果你真的想知道为什么,那是因为枚举是位域,这可能是测试或将任何类型的"选项"应用于方法的最快方法之一。

但是,该方法如何检查哪个方向有效?

因为如果我们有一个简单的枚举,我们正在检查是否等于 0、1、2 或 3 ...

但是,代码如何处理UIInterfaceOrientationMask?使用二进制 AND?

如果你需要测试最右边的位是否设置在二进制数0011中,你测试(3 & 1),这是真的(因为它基本上是(1 & 1)(。 因此,由于枚举只是命名的整数(您可能已经知道(,因此您可以通过 AND 来检查掩码中是否存在相关位,从而针对您感兴趣的任何一个或多个特定值测试完整的 OR'd 一起掩码。

在该特定示例中,它更进一步。 通过测试选项掩码中位的存在,以及整个选项,您可以获得"更安全"的测试,因为它保证选项的所有位都在那里。 对于更复杂的位掩码,这更有意义,但对于像UIInterfaceOrientationMask这样的简单掩码,简单的 AND 工作。

您的建议绝对有效:使用"按位 AND"运算符,然后检查结果是否为零(未设置目标位(。

作为背后的机器,你可以想象枚举就像一堆彩色笔。它们可以是连续的"红-橙-黄-绿..."作为彩虹,从那时起就没有限制提供确切的颜色,例如"橙蓝紫"。

因此,由您来选择分配给每个枚举成员的值(如果您不提供,编译器将根据自己的意愿分配下一个可用值(,决定您是否需要顺序或自定义顺序值等。

最新更新