[iOS SDK] UIDeviceOrientation ではまる

公開日: : iPad, iPhone

すぐURLが変更されそうだが、2013/02/16 時点だと、Supporting Multiple Interface Orientations という記事がiOS Developer Library にある。
View Controller Programming Guide for iOS: Supporting Multiple Interface Orientations

そこに、下記のようなコードがあり、UIDeviceOrientation を普通に使っている。

@implementation PortraitViewController
- (void)awakeFromNib
{
    isShowingLandscapeView = NO;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                 selector:@selector(orientationChanged:)
                                 name:UIDeviceOrientationDidChangeNotification
                                 object:nil];
}
 
- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
        isShowingLandscapeView = YES;
    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
             isShowingLandscapeView)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        isShowingLandscapeView = NO;
    }
}

このため、このコードをそのまま流用して画面回転対応コードを書いていたら、UIDeviceOrientation には UIDeviceOrientationFaceUp や UIDeviceOrientationFaceDown があるので、それに該当してしまい不具合を発生させてしまった。

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

ということで、UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
を使ったり、
– (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
を使ったりしよう。

iOS6 になって
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
がdeprecated になってしまったためにいろいろ混乱させられているが didRotateFromInterfaceOrientation などは普通に呼ばれているようだ。
何度も同じことではまっている気がする。

関連記事

ドラムちゃんに電子ドラムiWordを接続する

ドラム譜学習アプリ「ドラムちゃん」に、安価なMIDI電子ドラム iWord を接続する方法を説明し

記事を読む

no image

iPhone用 OmniGraffle ステンシル

ここのページを参考にして、iPhone用OmniGraffleステンシルを入手してみた。 確かにこれ

記事を読む

Apple Developer Program 更新2016

そろそろ期限が切れるので、Apple Developer Program - Apple Devel

記事を読む

no image

Let’s talk iPhone イベント 2011/10/04

今回のイベントも、楽しみにしながら寝て、早朝に起きてから知ることにした。 今回、事前に行われていた予

記事を読む

no image

アップルのタブレット端末に関するEngadgetのおもろい記事

Engadgetはごくたまに笑いのつぼにヒットするが今回のは面白かった。 ジョブズはタブレット端末に

記事を読む

iPad Pro 2017 12.9inch購入

新型コロナウィルスの影響でリモートワークの機会が増えているため、リモートワークをサポートす

記事を読む

no image

薄い iPod touch 4G用ケース SwitchEasy NUDE for iPod touch 4G UltraClear (SW-NUT4-UC) 購入

Retinaディスプレイと、ジャイロの確認のために購入したiPod touch 4G は開発用なので

記事を読む

[Xcode 9.3] iOS 11.3にしたら Xcode 9.3 + High Sierra が強制された

High Sierra はいろいろとアグレッシブな変更が入っており不安定と聞いていたので避けて通って

記事を読む

no image

インド式計算マスター

最近朝起きたらやっているアプリ。 これはいい。今まで全く知らなかったインド式計算がいつの日かマスター

記事を読む

Apple Special Event September 2018

今年も新型iPhoneを発表するアップルのスペシャルイベントが2018年9月13日2時AM(12日1

記事を読む

Message

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Break Back 22巻まで読了

テニス漫画と言えば、ベイビーステップスだと思っていたし、全巻持ってい

TORRAS iPhone 16 Pro Max用ケース

自分はiPhoneはケースを付けない派で、iPhone 12

iPhone 16 Pro Max 購入

iPhone 16 Pro Max 256GB 189000

Ember Mug 2のACアダプタをUSB Type-Cに変えてみた

冬になると活躍する Ember Mug 2 の充電器は付属のACアダ

Wi-Fi6Eルータ TP-Link AXE5400購入

Wi-Fi6E を試してみたくなり、TP-Link AXE5

→もっと見る

  • 2013年2月
     123
    45678910
    11121314151617
    18192021222324
    25262728  
PAGE TOP ↑