繁体   English   中英

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

[英]Understanding C++ enum through UIInterfaceOrientationMask for Objective-C / iOS

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

到目前为止,我了解。

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

因为如果我们有一个简单的枚举,我们将检查是否等于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运算掩码,以检查掩码中是否存在相关位。

在该特定示例中,它更进一步。 通过不仅测试选项掩码中是否存在该位,还测试整个选项,您将获得“更安全”的测试,因为它可以保证该选项的所有位都在那里。 对于更复杂的位掩码,这更有意义,但对于UIInterfaceOrientationMask这样的简单掩码,简单的ANDing则有效。

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

作为背后的机制,您可以想象枚举像彩色笔尖一样。 它们可以是连续的“红色-橙色-黄色-绿色...”,例如彩虹,因为这样对提供精确的颜色(例如“橙色-蓝色-紫色”)没有限制。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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