一.最近遇到了需求只在视屏播放页面允许横屏模式。 刚开始用了假旋转方案即手动把当前控制器的视图旋转 90度。但由于是假旋转 系统的导航栏还是竖屏状态,自定义导航栏本人不想用。于是发现了如下方法:
a.先设置屏幕旋转方向,每一项是啥意思大家自行百度,就没必要多说了
b.在 AppDelegate.h里面设置属性
//是否允许屏幕旋转
@property (nonatomic,assign)BOOL allowRotation;
在AppDelegate.m中
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {//当允许时,支持如下两个方向
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeRight;
}
//否则 就只有竖屏
return UIInterfaceOrientationMaskPortrait;
}
在视屏播放控制器的.m文件中这么写(其中有部分是使导航栏透明的代码)
//MARK: - viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//改变AppDelegate的appdelegete.allowRotation属性
AppDelegate *appdelegete = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegete.allowRotation = YES;
//设置系统导航栏默认透明度 才能使导航栏透明
self.navigationController.navigationBar.translucent = YES;
//设置导航栏背景图片为一个空的image,这样就透明了
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"HelveticaNeue" size:18.0], NSFontAttributeName, nil];
}
在视图将要消失的函数中把设置还原即可:
//MARK: - viewWillDisappear
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
AppDelegate *appdelegete = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegete.allowRotation = NO;
//关闭系统导航栏默认透明度 才能使导航栏颜色与设置的图片颜色一致
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"xzNavBar"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:COLORRGB(245, 245, 245, 1), NSForegroundColorAttributeName, [UIFont fontWithName:@"HelveticaNeue" size:18.0], NSFontAttributeName, nil];
}
到这里还没结束,本人重写了导航栏返回按钮的方法,如果是横屏状态下,点击返回按钮让其返回竖屏状态,只有竖屏时才给返回上一级页面。 (这里涉及到两个点:1.如何通过点击事件控制横竖屏。2.如果没有重写导航栏返回按钮的方法,在横屏模式下返回上一级页面会是横屏的模式,这不是我们想要的效果)于是:
c.我们可以在播放视屏控制器的上一级控制器中写下
//屏幕方向操作
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
如果播放视屏控制器上一级控制器比较多,你的项目中有基类的话。
在基类中写下:
//屏幕方向操作
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
并在播放视屏控制器中写下:
//屏幕方向操作
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeRight;
}
到此我们解决了问题 2。
如下我们解决问题1: 在播放视屏控制器中
#pragma mark - -- 屏幕旋转控制处理
- (void)tarnsformView:(BOOL)fullScreelBoll
{
if (fullScreelBoll) {//横屏
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
self.vlcPlayerView.frame = self.view.bounds;
self.webView.hidden = YES;
self.baseTableView.hidden = YES;
}else{//竖屏
[self interfaceOrientation:UIInterfaceOrientationPortrait];
self.vlcPlayerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16);
self.baseTableView.hidden = NO;
self.webView.hidden = NO;
}
}
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
//添加屏幕旋转状态监听
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)deviceOrientationDidChange
{
if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
//在竖屏状态下給视图控件重新修改其frame
self.vlcPlayerView.fullScreenBtn.selected = NO;
self.vlcPlayerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16);
self.baseTableView.hidden = NO;
self.webView.hidden = NO;
} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
//在横屏状态下給视图控件重新修改其frame
self.vlcPlayerView.fullScreenBtn.selected = YES;
self.vlcPlayerView.frame = self.view.bounds;
self.webView.hidden = YES;
self.baseTableView.hidden = YES;
}
}
// 到此横竖屏切换解决
对了:
// 隐藏状态栏显得和谐
- (BOOL)prefersStatusBarHidden
{
return YES;
}
二.导航栏透明效果。在上面的生命周期函数中已经附上代码(如下效果图)
Untitled.gif
网友评论