美文网首页
ZFPlayer跳坑比赛

ZFPlayer跳坑比赛

作者: 浪漫紫薇星 | 来源:发表于2018-06-15 00:32 被阅读733次

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;

最后我把这个方式去掉了就好了,只保持全屏后横屏就好了。

相关文章

  • ZFPlayer跳坑比赛

    ZFPlayer使用的很多,作者写的不错,很好用,但是也有跟自己项目不适合的地方,最坑的应该是限制横竖屏,我现在的...

  • iOS ZFPlayer 竖屏视频在真机下有声音无画面

    把pod 'ZFPlayer/AVPlayer', '~>4.0' 换成pod 'ZFPlayer/ijkplay...

  • iOS视频播放器

    ZFPlayer

  • 关于“跳坑”的

    坑不要跳!有些坑是跳不了的……去找那些我现在“跳不进去”的坑,然后挨个跳一下看看。不跳不知道坑有多大,俗话说机会总...

  • 跳坑

    自己的一生虽然有些波折,总的来说,还是一步一个脚印地走到今天,也认真践行了“天道酬勤”、“一份付出一份收获”,我对...

  • 跳坑

    周一加入新成员

  • 跳坑

    哈哈,这家伙又跳到坑里了,估计惨得很呀!现实生活中,普遍认为,跳坑是祸事,遇见都会避之不及! 其实,任何事...

  • 跳操比赛

    今天大课间的时候,我们举行了跳操比赛。 今天上学的时候,我们穿上了非常漂亮的衣服,而别的同学都穿的...

  • 跳长绳比赛

    星期四的时候我们进行了一场跳长绳比赛。 在这之前我们每天都在练习、选人,要按照我们每个人平均的速度来,...

  • 跳长绳比赛

    前几天体育老师在上操时宣布,近期体育课进行随堂跳长绳比赛。这可把我们班激动坏了,因为跳长绳对于我们班来说是一个非常...

网友评论

      本文标题:ZFPlayer跳坑比赛

      本文链接:https://www.haomeiwen.com/subject/esaueftx.html