[iOS SDK] UIDeviceOrientation ではまる
すぐ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 を接続する方法を説明し
-
-
iPhone用 OmniGraffle ステンシル
ここのページを参考にして、iPhone用OmniGraffleステンシルを入手してみた。 確かにこれ
-
-
Apple Developer Program 更新2016
そろそろ期限が切れるので、Apple Developer Program - Apple Devel
-
-
Let’s talk iPhone イベント 2011/10/04
今回のイベントも、楽しみにしながら寝て、早朝に起きてから知ることにした。 今回、事前に行われていた予
-
-
アップルのタブレット端末に関するEngadgetのおもろい記事
Engadgetはごくたまに笑いのつぼにヒットするが今回のは面白かった。 ジョブズはタブレット端末に
-
-
iPad Pro 2017 12.9inch購入
新型コロナウィルスの影響でリモートワークの機会が増えているため、リモートワークをサポートす
-
-
薄い 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 はいろいろとアグレッシブな変更が入っており不安定と聞いていたので避けて通って
-
-
インド式計算マスター
最近朝起きたらやっているアプリ。 これはいい。今まで全く知らなかったインド式計算がいつの日かマスター
-
-
Apple Special Event September 2018
今年も新型iPhoneを発表するアップルのスペシャルイベントが2018年9月13日2時AM(12日1