iOS视频全屏与界面旋转的控制

时间:2022-01-13 15:50:18

一般来说都是用navigation来控制界面的跳转

就需要这样一段代码来控制界面的旋转


- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
UIViewController* vc = [self topViewController];
if ([vc respondsToSelector:@selector(supportedInterfaceOrientations)]) {
UIInterfaceOrientationMask interface = [vc supportedInterfaceOrientations];
return interface;
}
return UIInterfaceOrientationMaskPortrait;
}

而6的那个旋转返回方法无视就好

而如果是使用presentViewController来控制界面的跳转的话可以不要这段代码、写这段代码就是为了控制所有界面可以旋转的方向、在没有视频播放的界面可以设置成UIInterfaceOrientationMaskPortrait、在视频播放界面设置成UIInterfaceOrientationMaskAllButUpsideDown

当然如果真的一个界面一个界面的去设置、那我就无话可说了

而在视频播放界面可以这样操作全屏方法


- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
//这个方法可以不要已无需再适配6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"left");
[self landscape];
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"right");
[self landscape];
break;
case UIInterfaceOrientationPortrait:
NSLog(@"portrait");
[self portrait];
break;
default:
break;
}
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"left");
[self landscape];
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"right");
[self landscape];
break;
case UIInterfaceOrientationPortrait:
NSLog(@"portrait");
[self portrait];
break;
default:
break;
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {

}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
- (void)portrait {
isFullScreen = NO;
[self originPlayFrame];
}
- (void)landscape {
isFullScreen = YES;
[self fullScreenFrame];
}

而如果有全屏按钮的话可以在全屏按钮里面写入这样一段话


- (void)makeFullScreen:(UIButton *)sender {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
if (isFullScreen) {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
}
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
这样就完成了

其中全屏界面的frame自己调整好就可以了