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のバージョン上がったら使えなくなるかもしれない。