我通过以下方式检测屏幕更改方向:
[[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 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];
}
}