[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 などは普通に呼ばれているようだ。
何度も同じことではまっている気がする。

関連記事

LogLocations 1.3.2 をリリースしました

LogLocations 1.3.2をリリースしました。審査後、近日中に公開されます。下記のレビュー

記事を読む

no image

[iPhone SDK] View Based Application で NIB(XIB)ファイルを削除してみる

iPhoneアプリ開発に慣れてくると、段々Interface Builderを使わなくなってくる。

記事を読む

[iPhone 6 Plus] TUNEWEAR の iPhone 6 Plus 用ケースを買ってみた

iPhone 6 Plus 購入時に、とりあえず購入した Simplism のケースは、すぐ

記事を読む

開発用iPad比較検討2016

手持ちのiPad2およびiPad mini 初代がiOS10のサポート対象から外れてしまったので、i

記事を読む

no image

最近読んだ本 iPhoneデジカメプログラミング

カメラアプリを作る予定はなかったので2011年3月に発売されてからしばらく様子を見ていたが、そろそろ

記事を読む

no image

Pebble E-Paper Watch 届いた! 感想。

追記: Pebble 向けにアプリを作りました。 Kickstarter で iPhone/A

記事を読む

no image

SHIELD iShell iPhone 4 用ケース購入

奥さんのiPhone 4用に SHIED iShell iPhone 4 シェルカバーを購入。Ama

記事を読む

no image

WWDC 2010 開幕

去年と違って今年はすっかりTwitterが一般化しているので、WWDCに行く人たちの様子がよくわかる

記事を読む

no image

Corona SDK 調査2日目

昨日はとりあえずCorona SDKを使って付属のLuaのサンプルアプリをiPhone実機上で動かし

記事を読む

リズムくんHD 1.2.1 で iOS11に対応しました

iPad用のリズムくんHD のiOS11対応が遅れてしまったため、2017年11月あたりからiOS1

記事を読む

Message

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

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Apple Developer Program更新 2024

今年も更新した。 昨年はブログに記録し忘れたらしい。 今

ポモドーロテクニック用物理タイマーならTime Timer

会社ではなかなか自由に時間を使えないが、家で読書や作業をする

DELL 32インチディスプレイ U3223QE 購入

Dell U3223QE は解像度 3840x216

WWDC 2023 Vision Pro発表

2023/6/5 (日本時間 2023/06/06 2AM)のWWD

M1 MacBook Air を Venturaにアップデートする

M1 MacBook Air を macOS Montere

→もっと見る

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