注:文章看过读过很多,从来没有自己写过,有写的不好的地方请谅解!谢谢
由于项目中APP不支持横屏,但是单一某个视频界面需要支持横屏以及状态栏横向
正常显示(竖屏)
注:由于为公司项目,当前截图只是截取需要操作的界面的View
横屏:
为了更好的理解这个视图,我画了一个图还解释(需要横屏显示的及绿色部分,在竖屏正常显示的时候界面就如下):
废话不多少,直接上代码:
我的当前项目的需求是点击全屏按钮之后,横屏显示界面
#pragma mark -- 全屏按钮
- (void)mainScreenAction:(UIButton *)btn{
DLog(@"全屏按钮");
isHalfScreen= !isHalfScreen;
if(isHalfScreen){//进入全屏
[self enterFullscreen];
}else{
//退出全屏
[self exitFullscreen];
}
}
//进入全屏
#pragma mark -- 进入全屏
- (void)enterFullscreen {
/** 记录进入全屏前的parentView和frame*/
openGLView.movieViewParentView = openGLView.superview;
openGLView.movieViewFrame = openGLView.frame;
/** movieView移到window上*/
CGRect rectInWindow = [openGLView convertRect:openGLView.bounds toView:[UIApplication sharedApplication].keyWindow];
[openGLView removeFromSuperview];
openGLView.frame = rectInWindow;
[[UIApplication sharedApplication].keyWindow addSubview:openGLView];
[sliderView removeFromSuperview];
/** 执行动画*/
[UIView animateWithDuration:0.5 animations:^{
openGLView.transform = CGAffineTransformMakeRotation(M_PI_2);
openGLView.bounds = CGRectMake(0, 0, CGRectGetHeight(openGLView.superview.bounds), CGRectGetWidth(openGLView.superview.bounds));
openGLView.center = CGPointMake(CGRectGetMidX(openGLView.superview.bounds), CGRectGetMidY(openGLView.superview.bounds));
} completion:^(BOOL finished) {
//退出全屏按钮位置
sliderView.frame = CGRectMake(openGLView.bounds.size.width-65, openGLView.bounds.size.height-50, 40, 40);
[mainScreenBtn setImage:[UIImage imageNamed:@"退出全屏.png"] forState:UIControlStateNormal];
[openGLView addSubview:sliderView];
}];
[self refreshStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
//显示状态栏
[[UIApplication sharedApplication] setStatusBarHidden:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
//退出全屏
#pragma mark -- 退出全屏
- (void)exitFullscreen {
CGRect frame11 = [openGLView.movieViewParentView convertRect:openGLView.movieViewFrame toView:[UIApplication sharedApplication].keyWindow];
[UIView animateWithDuration:0.5 animations:^{
[sliderView removeFromSuperview];
openGLView.transform = CGAffineTransformIdentity;
openGLView.frame = frame11;
} completion:^(BOOL finished) {
/*
* movieView回到小屏位置
*/
[openGLView removeFromSuperview];
openGLView.frame = openGLView.movieViewFrame;
[openGLView.movieViewParentView addSubview:openGLView];
//退出全屏按钮位置
sliderView.frame = CGRectMake(openGLView.bounds.size.width-65, openGLView.bounds.size.height-50, 40, 40);
[mainScreenBtn setImage:[UIImage imageNamed:@"全屏.png"] forState:UIControlStateNormal];
[openGLView addSubview:sliderView];
}];
[self refreshStatusBarOrientation:UIInterfaceOrientationPortrait];
//显示状态栏
[[UIApplication sharedApplication] setStatusBarHidden:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
- (void)refreshStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation {
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES];
}
这里在实现了视图界面横屏之后,会出现状态栏依然是竖屏显示的状态。所以还要做以下操作(因为项目框架是UINavigationController搭建的,故在两个控制器里都需要加以下代码)
UINavigationController:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotate{
return [[self.viewControllers lastObject] shouldAutorotate];
}
然后在需要横屏的控制器里加入代码
- (BOOL)shouldAutorotate {
return NO;
}