iOS中仅当视屏全屏播放时支持视屏旋转

来源:互联网 发布:软考程序员百度云 编辑:程序博客网 时间:2024/05/19 20:47

开发环境:XCode7.2 

项目设置:Device Orientation仅勾选Portrait    Deployment Target7.0  使用MediaPlayer

需求:在视屏进入全屏播放时,视屏视图旋转,达到真的全屏播放

实现:①在AppDelegate.h文件中声明一个BOOL类型的变量,记录是否允许旋转:

             @property (nonatomic, assign) BOOL  allowRotation;

          ②在AppDelegate.m文件中添加方法内容:(方法为系统提供)

            - (UIInterfaceOrientationMask) application: (UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

                       if(self.allowRotation) {

                               //无特殊要求,故直接返回支持所有方向旋转

                               return UIInterfaceOrientationMaskAll;

                      }else{

                    //当不允许旋转的时候,只支持默认的方向              

                               return UIInterfaceOrientationMaskPortrait;

                     }

           }

        ③在有播放器的viewController中导入AppDelegate

            #import"AppDelegate.h"

        ④添加观察者方法:(自己定义,定以后可在viewDidLoad中调用)

           - (void)addNotification {

                    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

                   [center addObserver:self selector:@selector(willEnterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];//进入全屏的通知

                  [center addObserver:self selector:@selector(willExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];//退出全屏的通知

}

- (void)willEnterFullScreen:(NSNotification*)notification

{

       AppDelegate*delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

      //设置在进入全屏时,允许旋转

      delegate.allowRotation=YES;

}

- (void)willExitFullScreen:(NSNotification*)notification

{

      AppDelegate*delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

     //设置退出全屏模式禁止旋转

      delegate.allowRotation=NO;

}

//释放观察者

- (void)dealloc {

      [[NSNotificationCenter defaultCenter] removeObserver:self];

}

通过以上方法达到了项目仅支持原始方向的竖屏,但当播放器进入全屏模式的时候,可以旋转播放器,使视频全屏播放

0 0
原创粉丝点击