ZFPlayer跳坑比赛
ZFPlayer使用的很多,作者写的不错,很好用,但是也有跟自己项目不适合的地方,最坑的应该是限制横竖屏,我现在的需求是在WebView里面的播放器全屏需要横屏,一个分类"UIViewController+ZFPlayerRotation.h"限制了所有ViewController的横竖屏,这样如果需要一个vc横屏,就会有问题,而且播放器必须需要
- (BOOL)shouldAutorotate {
return NO;
}
不然播放器不能自动横竖屏,作者给的注释“默认所有都不支持转屏,如需个别页面支持除竖屏外的其他方向,请在viewController重新下边这三个方法”
// 是否支持自动转屏
- (BOOL)shouldAutorotate {
return NO;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
重写这三个方法根本无效,因为这是分类里面限制了,所以干脆不用这个分类,换个思路,既然必须 shouldAutorotate return NO;- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait;}
那么我就在AppDelegate里面控制,首先我不需要启动页横屏,我直接
只勾选Portrait不然横屏启动会出现页面错乱,在APPDelegate.h文件中增加属性:是否支持横屏
/*** 是否允许横屏的标记 */
@property (nonatomic,assign)BOOL allowRotation;
在APPDelegate.m文件中增加方法,控制全部不支持横屏
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
这样在其他界面想要横屏的时候,我们只要控制allowRotation这个属性就可以控制其他界面进行横屏了。
需在上面文件里
#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
不让横屏的时候
appDelegate.allowRotation = NO;即可
下面来说说我的项目问题,上面的方法可以让页面横屏,但是h5的页面里面的播放器全屏的时候还是不是横屏,那么我就用了下面的方式强制横屏。
通过Reveal我们可以查看到view的一些层级关系,可以看出弹出播放的是AVPlayerView
监听两个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
#pragma - mark 进入全屏
-(void)begainFullScreen
{
if(!self.didWebViewLoadOK) {
return;
}
AppDelegate*appDelegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
appDelegate.allowRotation=YES;
[[UIDevice currentDevice] setValue:@"UIInterfaceOrientationLandscapeLeft" forKey:@"orientation"];
//强制zhuan'p:
if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {
SELselector =NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocationsetSelector:selector];
[invocationsetTarget:[UIDevicecurrentDevice]];
int val = UIInterfaceOrientationLandscapeLeft;
[invocationsetArgument:&valatIndex:2];
[invocationinvoke];
}
}
#pragma - mark 退出全屏
-(void)endFullScreen
{
AppDelegate*appDelegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
appDelegate.allowRotation=NO;
//强制归正:
if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {
SELselector =NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocationsetSelector:selector];
[invocationsetTarget:[UIDevicecurrentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocationsetArgument:&valatIndex:2];
[invocationinvoke];
}
}
如果横屏启动有的页面会变形,加上这句
[[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait];
我里面又有弹出页面,这样状态栏会单独发生横屏,
AppDelegate*appDelegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
appDelegate.allowRotation=YES; appDelegate.allowRotation=NO;
最后我把这个方式去掉了就好了,只保持全屏后横屏就好了。