注:文章看過讀過很多耗美,從來沒有自己寫過,有寫的不好的地方請諒解!謝謝
由于項目中APP不支持橫屏米死,但是單一某個視頻界面需要支持橫屏以及狀態(tài)欄橫向
正常顯示(豎屏)
注:由于為公司項目衷佃,當(dāng)前截圖只是截取需要操作的界面的View
橫屏:
為了更好的理解這個視圖趟卸,我畫了一個圖還解釋(需要橫屏顯示的及綠色部分,在豎屏正常顯示的時候界面就如下):
廢話不多少氏义,直接上代碼:
我的當(dāng)前項目的需求是點擊全屏按鈕之后锄列,橫屏顯示界面
#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];
/** 執(zhí)行動畫*/
[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];
//顯示狀態(tài)欄
[[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];
//顯示狀態(tài)欄
[[UIApplication sharedApplication] setStatusBarHidden:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
- (void)refreshStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation {
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES];
}
這里在實現(xiàn)了視圖界面橫屏之后,會出現(xiàn)狀態(tài)欄依然是豎屏顯示的狀態(tài)惯悠。所以還要做以下操作(因為項目框架是UINavigationController搭建的右蕊,故在兩個控制器里都需要加以下代碼)
UINavigationController:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotate{
return [[self.viewControllers lastObject] shouldAutorotate];
}
然后在需要橫屏的控制器里加入代碼
- (BOOL)shouldAutorotate {
return NO;
}