0

我检测屏幕改变方向:

    [[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];
    }
}
4

0 回答 0