定食屋おろポン

おろしポン酢と青ネギはかけ放題です

iOS5以下に配慮した、iOS6での画面回転制御コードメモ

これまで回転の可不可を判別していた

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

iOS非推奨になり、plistと

- (BOOL)shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

ここらへんのメソッドで回転を制御するようになりましたね。
多分こんなふうに書いておけば、iOS5以下でも動作し、かつiOS6で追加されたメソッドでの実装に集中できるんではないかなーと思いメモ。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (![self shouldAutorotate]) {
        return NO;
    }
    
    NSUInteger  toInterfaceOrientationMask;
    switch (toInterfaceOrientation) {
    case UIInterfaceOrientationPortrait:
        toInterfaceOrientationMask = UIInterfaceOrientationMaskPortrait;
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        toInterfaceOrientationMask = UIInterfaceOrientationMaskPortraitUpsideDown;
        break;
    case UIInterfaceOrientationLandscapeLeft:
        toInterfaceOrientationMask = UIInterfaceOrientationMaskLandscapeLeft;
        break;
    case UIInterfaceOrientationLandscapeRight:
        toInterfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
        break;
    default:
        return NO;
    }
    
    return ([self supportedInterfaceOrientations] & toInterfaceOrientationMask);
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger  interfaceOrientations;

    // iPhone
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        interfaceOrientations = UIInterfaceOrientationMaskAllButUpsideDown;
    }
    // iPad
    else {
        interfaceOrientations = UIInterfaceOrientationMaskAll;
    }
    
    return interfaceOrientations;
}

ビット演算などを使ってコード量を減らす努力をしてもいいんだけど、これくらい愚直な方が読みやすい気がする。