iPhone 横竖屏切换,全屏播放的三种方式
Posted 星红
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iPhone 横竖屏切换,全屏播放的三种方式相关的知识,希望对你有一定的参考价值。
1. 调用系统自带的强制屏幕旋转不过还得在AppDelegate中重写下面方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { // 0:竖屏,1:单向横屏,2:双向横屏 if (self.httpConnect.SupportedOrientation==0) { return UIInterfaceOrientationMaskPortrait; } else if (self.httpConnect.SupportedOrientation==1){ 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(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH); [self hideNavigationAndTabBar:YES]; self.httpConnect.SupportedOrientation = 1; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:@"orientation"]; } - (void)backToPortraitMode { [UIView animateWithDuration:0.2 animations:^{ self.httpConnect.SupportedOrientation = 0; [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(0, 0, 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/2); CGRect frame = [UIScreen mainScreen].applicationFrame; self.view.bounds = CGRectMake(0, 0, 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]; }
以上是关于iPhone 横竖屏切换,全屏播放的三种方式的主要内容,如果未能解决你的问题,请参考以下文章