部分页面旋转

1.首先项目允许旋转


WX20170506-193421.png

2.在AppDeleGate里面添加监听

// 播放器即将播放通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    // 播放器即将退出通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];

3.设置AppDelegate属性和方法

//AppDelegate.h
{
    BOOL _isFull; // 是否全屏
}
@property (nonatomic)BOOL isFull;

//AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if(_isFull){
    return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}

4.在需要旋转的界面

- (void)ZJVideoPlayerView_isFullScreen:(BOOL)isFullScreen{
 
    if (isFullScreen) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
        [self rotateScreen:isFullScreen];
        self.videoPlayerView.sd_layout
        .topSpaceToView(self.view, 0)
        .leftSpaceToView(self.view, 0)
        .rightSpaceToView(self.view, 0)
        .heightIs(self.view.width / 16 * 9);
        [self.videoPlayerView updateLayout];
        
        [self.view bringSubviewToFront:self.videoPlayerView ];
    }else{
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
        [self rotateScreen:isFullScreen];
        self.videoPlayerView.sd_layout
        .topSpaceToView(self.view, 64)
        .leftSpaceToView(self.view, 0)
        .rightSpaceToView(self.view, 0)
        .heightIs(self.view.width / 16 * 9);
        [self.videoPlayerView updateLayout];
        [self.view sendSubviewToBack:self.videoPlayerView];
    }
}
//旋转界面
- (void)rotateScreen:(BOOL)isLandscapeRight
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        NSNumber *num = [[NSNumber alloc] initWithInt:(isLandscapeRight?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait)];
        [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)num];
        [UIViewController attemptRotationToDeviceOrientation];
        //这行代码是关键
    }
    SEL selector=NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val =isLandscapeRight?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
    [[UIApplication sharedApplication] setStatusBarHidden:isLandscapeRight withAnimation:UIStatusBarAnimationSlide];
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,670评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,491评论 19 139
  • 《裕语言》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 10...
    叶染柒丶阅读 28,457评论 5 19