次の方法で画面変更の向きを検出します。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
そして、それが呼び出されると、このメソッドを呼び出します:
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
[md changeToBigNowPlaying:YES withLayer:avPlayerLayer];
}else{
[md changeToBigNowPlaying:NO withLayer:avPlayerLayer];
}
そして、この関数で私はこのようなことをします:
-(void)changeToBigNowPlaying:(BOOL)flag withLayer:(AVPlayerLayer*)layer{
if (flag) {
//To portrait
[landVC.view removeFromSuperview];
[layer removeFromSuperlayer];
[self.window addSubview:tab.view];
}else {
//To landscape
[tab.view removeFromSuperview];
[landVC setUpNewLayer:layer];
[self.window addSubview:landVC.view];
}
}
これが説明です:
ビューコントローラー付きのタブバーがあり、ユーザーが画面を横向きに回転したことを検出したら、タブバーを削除して UIViewController(landVC) を追加し、再び縦向きに回転させたら、landvc を削除してタブバーを再度追加します。
私の問題は、最初に回転するときです。UIInterfaceOrientationLandscapeLeft
そのため、画面が に表示されUIInterfaceOrientationLandscapeRight
ます。しかし、UIInterfaceOrientationLandscapeRight
最初に回転すると、本来あるべき画面が表示され、この状況から回転するとUIInterfaceOrientationLandscapeLeft
、画面も良好に表示されます。
UIInterfaceOrientationLandscapeLeft
に回転してlandVcが表示されたときに問題が発生する理由を考えてくださいUIInterfaceOrientationLandscapeRight
landVC UIViewController では、次を実装します。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight){
return YES;
}else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return YES;
}else {
return NO;
}
}
編集
-(void)detectOrientation{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
[md changeToBigNowPlaying:YES withLayer:avPlayerLayer];
}
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
[md changeToBigNowPlaying:NO withLayer:avPlayerLayer];
}
}