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

関連記事

Photoshop のバッチ処理でiOSアプリアイコンを一括作成する

毎回苦労しているiOSのアプリアイコン作成。今回はPhotoshopのバッチで作成してみる。前回は 

記事を読む

no image

iPhone アプリアイコン

iphoneアプリで稼げるのか さんと同様、自分もアイコンを作るのはつらい。 今度アプリを公開すると

記事を読む

no image

iPhoneアプリビジネス本 The Business of iPhone App Development

iPhoneアプリを売るための情報が詰まった本。 ここまでやるか、というくらいの情報が詰まっている。

記事を読む

no image

iPhone SDK勉強会

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

記事を読む

TestFlight を使ったベータテストについて

アプリのベータテストにご協力いただきありがとうございます。ベータテストには、AppleのTestFl

記事を読む

no image

おんぷちゃん for iPad 1.3.3 Submit

今回は紅白歌合戦を見ながらSubmit。 相変わらずうちの娘が譜読みに苦労していて新しい曲を弾く

記事を読む

no image

[iPhone開発本][洋書] iPhone 3D Programming

たまたまApp Storeで O'reilly Media で検索したら、600円の本がわんさか表示

記事を読む

[iOS SDK] No identities are available for signing 問題にはまる

久しぶりに Provisioning Profileではまった。 この問題にはまると、解決までにか

記事を読む

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

2015年から作りはじめた iPad専用 ドラム譜学習アプリ ドラムちゃん を本日 2017/01/

記事を読む

no image

UIScrollView の上で UIView を動かしたい

今作っているiPadアプリで、画面をピンチインアウトで拡大縮小して、さらにその上でドラッグで部品を動

記事を読む

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 ↑