横竖屏切换

广告:系统方法监听横竖屏,前提是要开启了横竖屏才可以用。iPad上可能有点用。

方法1,别人封装的一个导航,跳转vc时套上即可横竖屏切换ChangeOrientation
详细介绍
方法2,自己动手实现,切换播放器时的例子。
关闭横竖屏
- (void)viewDidLoad {
    [super viewDidLoad];
    //监听横竖屏
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}

//监听横竖屏切换的通知
- (void)deviceOrientationDidChange
{
    NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
    if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        //往左侧,全屏
        [self orientationChange:NO];
        //旋转屏幕改变播放器frame(如果是约束,这部可以省了。)
        self.playerView.frame=CGRectMake(0, 0, self.view.bounds.size.width, 200);
        
    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
        //往右侧,恢复原始大小
        [self orientationChange:YES];
        //旋转屏幕改变播放器frame(如果是约束,这部可以省了。)
        self.playerView.frame=self.view.bounds;
    }
}


#pragma mark - 手动旋转当前view
- (void)orientationChange:(BOOL)landscapeRight
{
    if (landscapeRight) {
        [UIView animateWithDuration:0.2f animations:^{
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
            self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    } else {
        [UIView animateWithDuration:0.2f animations:^{
            self.view.transform = CGAffineTransformMakeRotation(0);
            self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    }
}

/*
    关闭自动旋转
    在设备出于横屏时,界面就会变成横屏;
    设备处于竖屏时,界面就会变成竖屏。
 */
- (BOOL)shouldAutorotate
{
    return NO;
}
/*
    移除通知,防止内存泄漏。
*/
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


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

推荐阅读更多精彩内容