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 をどのように扱っているのでしょうか? バイナリ 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
}
その通りですか?
ありがとう