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

関連記事

US Unlocked にてiPad 購入、無事入手

日本の発売まで待つか結構悩んだけれども、なるべく早く自作アプリの動作確認をしておきたいということもあ

記事を読む

no image

[iPhone 開発本] iPhone/iPadゲーム開発ガイド ―Objective-Cで作る2D/3Dゲーム

今年の4月にオライリーのiPhoneゲーム開発本 iPhone Game Development (

記事を読む

no image

Learn iPhone and iPad Cocos2D Game Development

洋書だけれども、ApressからCocos2d を使ってゲーム開発する本として Learn iPho

記事を読む

コードちゃん 1.1.0 で五度圏学習機能をサポート → 新アプリに分離しました

お知らせ ほとんど使われていないのと、1.2.0でコード進行問題を追加したため、1.2.0

記事を読む

no image

iPhoneアプリ 「リズムくん」 Ver.1.1 アップデート

リズムくん Ver.1.0ではまずは8分音符までの問題でリリースしてみましたが、やはりより難しい問題

記事を読む

no image

Lexical or Preprocessor issue ‘xxx.h’ file not found

NSMutableArray に要素をランダムに並び替える機能を入れようと思い、 objective

記事を読む

EverLearn-Pebble

2016年の総括

2015年の振り返り を振り返ってみて興味深かったので、2016年も書いてみる。個人的には、2016

記事を読む

no image

iPhone SDK勉強会

iPhone 開発の勉強会をしよう、ということになったので、それ向けにメモを書いてみる。相手はいろい

記事を読む

no image

App Store レビュー中

8月13日にApp StoreにSubmitしたiPhoneアプリがまだIn Review状態だ。果

記事を読む

新アプリ「コードちゃん」を公開しました

速報:新 iPad 発表。Apple Pencil対応で3万7800円から - Engadget

記事を読む

Message

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

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

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

児童手当 認定請求書申請 2024 「請求者が養育をする18歳に達する日以降の最初の3月31日までの子の数」とは?

2024年に受給していない人には手紙が届くらしい。 電子申請も

→もっと見る

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