0

私はビットごとに少し迷っています;)

私の目標は、アプリケーションでサポートされている向きのセット全体を取得し、各結果値をテストしてカスタム変数を更新することです。私の問題は、比較を行う方法がわからないことです(変換/テストの問題が発生しました...)

最初にこの記事を読みました:ビットごとの列挙値のテスト しかし、それは私に光をもたらしません...

アプリケーションに対して次の方向宣言があるとします (以下は変数supportedOrientationsのログ出力です): supported orientations = ( UIInterfaceOrientationPortrait )

したがって、私の最初の試みは整数値でいくつかのテストを試みることでしたが、機能しません(アプリケーションがポートレートモードであると宣言されていても、テストは「false」を返します):

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
NSLog(@"[supported orientations = %@", supportedOrientations);
// for clarity just make a test on the first orientation we found
if ((NSInteger)supportedOrientations[0] == UIInterfaceOrientationPortrait) {
  NSLog(@"We detect Portrait mode!");
}

私の 2 番目の試みは、ビットごとのことを試すことでしたが、今回は常に「true」を返します (サポートされている方向が UIInterfaceOrientationPortrait でない場合でも)。:

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
NSLog(@"[supported orientations = %@", supportedOrientations);
// for clarity just make a test on the first orientation we found
if ((NSInteger)supportedOrientations[0] | UIInterfaceOrientationPortrait) { // <-- I also test with UIInterfaceOrientationMaskPortrait but no more success
  NSLog(@"We detect Portrait mode!");
}

だから私の質問は:

  • 私の場合、向きをテストするにはどうすればよいですか?

  • ビットごとのもの(|オペランドを使用)を使用してテストを使用する方法ですか?

4

1 に答える 1

0

公式ドキュメントによると、UISupportedInterfaceOrientations文字列の配列ですhttps://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW10

したがって、解決策は、配列で見つかった各要素に対して NSString 比較を使用することです。

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
for (NSString *orientation in supportedOrientations) {        
    if ([orientation isEqualToString:@"UIInterfaceOrientationPortrait"] ||
        [orientation isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
        NSLog(@"*** We detect Portrait mode!");
    } else if ([orientation isEqualToString:@"UIInterfaceOrientationLandscapeLeft"] ||
               [orientation isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
        NSLog(@"*** We detect Landscape mode!");
    }
}

このようにして、(UIInterfaceOrientation 型の) enum 値を利用しなかったことに注意してください。

于 2013-09-30T09:32:01.063 に答える