at iPhoneOS 3.1.2

shouldAutorotateToInterfaceOrientationじゃなくて任意のタイミングで roateする方法を探していたんだけど、見かけるのはUIViewのtransformでrotateすることばかり。確かめてないけど。この方法だとUIViewControllerのinterfaceOrientationと齟齬も発生するじゃないかと思う。

それで無理くり何とかする方法を考案した。けど、この方法では(もう少し改善すればできるかもしれないが)重力方向への回転しかできない。 shouldAutorotateToInterfaceOrientation:を使ってrotateすのはいつもとどおり。だけど先に方向を決めておいてNSNotificationを投げて強制的にshouldAutorotateToInterfaceOrientation:を発生させることによって 任意のタイミングでviewを回転させる事ができる。

   1  - (void)rotateNow{
   2    UIDevice *device = [UIDevice currentDevice];
   3    self.toRotate = device.orientation;
   4    NSNotification *notification = [NSNotification notificationWithName:@"UIDeviceOrientationDidChangeNotification" object:device];
   5    [[NSNotificationCenter defaultCenter] postNotification:notification];
   6  }
   7  
   8  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   9    if(self.toRotate){
  10      return interfaceOrient == self.toRotate;
  11    }else{
  12      return NO;
  13    }
  14  }

こんな感じ。何をしたかったかと言うと向きが変わったときにいきなり回転させるんじゃなくて、ちょっとボタンをだして確認を取りたかった。だからタイミングだけずらして回転方向は重力方向だけでいいやって話しなんだけど。

もしかすると、このNSNotificationを作ってる部分でobject:deviceってUIDeviceを渡してる部分。このオブジェクトのプロパティorientationを任意の方向を返す様なオブジェクトを作ってViewControllerを更に騙すことができるならば、タイミングだけじゃなくて方向も変えれるんじゃないかな?そこまでは確認を取ってません。

まぁ何にせよ。かなりhackな方法だからOSのバージョン上がったら使えなくなるかもしれない。

posted by Png bopper on Sun 31 Jan 2010 at 14:02
Contents
UIViewControllerだけで強制的にrotateする方法。
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ