
正文
iPhone 横竖屏切换,全屏播放的三种方式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1. 调用系统自带的强制屏幕旋转不过还得在AppDelegate中重写下面方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
// 0:竖屏,1:单向横屏,2:双向横屏
if (self.httpConnect.SupportedOrientation==) {
return UIInterfaceOrientationMaskPortrait;
}
else if (self.httpConnect.SupportedOrientation==){
return UIInterfaceOrientationMaskLandscapeRight;
}
else{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
在Viewcontroller中添加下面方法
-(BOOL)shouldAutorotate
{
return NO;
}
- (void)hideNavigationAndTabBar:(BOOL)state {
[self.navigationController.navigationBar setHidden: state];
[self.tabBarController.tabBar setHidden: state];
[[UIApplication sharedApplication] setStatusBarHidden:state];
}
- (void)gotoFullScreenMode { self.movieView.frame = CGRectMake(, , SCREEN_HEIGHT, SCREEN_WIDTH); [self hideNavigationAndTabBar:YES];
self.httpConnect.SupportedOrientation = ; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:@"orientation"];
}- (void)backToPortraitMode { [UIView animateWithDuration:0.2 animations:^{ self.httpConnect.SupportedOrientation = ;
[self hideNavigationAndTabBar:NO]; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
} completion:nil];
}
2. 全屏播放利用[UIApplication sharedApplication].keyWindow,主要代码如下
//不过这种方式麻烦一点,全屏时把movieView从self.view中移到keyWindow当中去;退回全屏时又得把movieView从keyWindow中加到movieView中去,还得重新考虑布局,不推荐这种方式;
iOS8 之后 [UIScreen bounds] 的宽高会随屏幕方向改变
- [UIScreen bounds] now interface-oriented
- [UIScreen applicationFrame] now interface-oriented
#define AppKeyWindow ([UIApplication sharedApplication].keyWindow)
- (void) gotoPlayVideoInFullScreen { [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; [AppKeyWindow addSubview: self.movieView];
self.movieView.transform = CGAffineTransformMakeRotation(M_PI_2);
self.movieView.frame = CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT);
}- (void)backToNormalState { [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation: UIStatusBarAnimationSlide];
self.movieView.transform = CGAffineTransformIdentity;
[self.movieView removeFromSuperview]; [self.view addSubview:self.movieView];
}
3. 第三种最方便快捷的,直接对self.view进行动画旋转,代码如下
- (void)gotoPlayVideoInLandscapeMode{ [self hideNavigationAndTabBar:YES];
self.view.transform = CGAffineTransformMakeRotation(M_PI/);
CGRect frame = [UIScreen mainScreen].applicationFrame;
self.view.bounds = CGRectMake(, , frame.size.height, frame.size.width);
}- (void)gotoPlayVideoInPortraitMode { self.view.transform = CGAffineTransformIdentity;
[self hideNavigationAndTabBar:NO];
[self.view layoutIfNeeded];
}- (void)hideNavigationAndTabBar:(BOOL)state { [self.navigationController.navigationBar setHidden: state];
[self.tabBarController.tabBar setHidden: state];
[[UIApplication sharedApplication] setStatusBarHidden:state];
}







