设备轮换问题

时间:2012-04-30 13:40:58

标签: iphone objective-c

我通过以下方式检测屏幕更改方向:

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

这就是解释:

我有带有viewcontroller的tabbar,当我检测到用户将屏幕旋转到横向时,我删除了tabbar并添加了一个UIViewController(landVC),当我再次将其旋转为肖像时,我删除了landvc并再次添加了tabbar。

我的问题是,当我先转到UIInterfaceOrientationLandscapeLeft时,它会显示UIInterfaceOrientationLandscapeRight中的屏幕。但是当我首先旋转到UIInterfaceOrientationLandscapeRight时,我会看到它应该的屏幕,当我从这种情况旋转到UIInterfaceOrientationLandscapeLeft时,我看到屏幕也很好。

当我转到UIInterfaceOrientationLandscapeLeft并且在UIInterfaceOrientationLandscapeRight

中看到landVc时,我知道为什么会出现问题

在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];
    }
}

0 个答案:

没有答案