您的位置:首页 > 移动开发 > IOS开发

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

2016-03-30 14:50 417 查看
开发环境: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{

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

                               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];

}

通过以上方法达到了项目仅支持原始方向的竖屏,但当播放器进入全屏模式的时候,可以旋转播放器,使视频全屏播放
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: