视频播放如何横竖屏切换续

5 篇文章 0 订阅

接上篇文章

2 腾讯视频的横竖屏切换的实现方法

首先控制整个viewcontroller的view仅支持默认的竖屏即可

- (BOOL)shouldAutorotate{
    return NO;
}

然后添加监测屏幕方向变化的通知

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationHasChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

接着在通知的回调方法中操作如下

- (void)orientationHasChange:(NSNotification *)notification{
    UIDevice *device = (UIDevice *)notification.object;
    if(device.orientation == UIInterfaceOrientationLandscapeLeft){
        [self rotateToLandscapeLeft];
    }
    else if(device.orientation == UIInterfaceOrientationLandscapeRight){
        [self rotateToLandscapeRight];
    }
    else if(device.orientation == UIInterfaceOrientationPortrait){
        [self rotateToPortrait];
    }
}
- (void)rotateToFullScreen{
    if(SCREEN_WIDTH>SCREEN_HEIGHT){
        _videoPlayManageView.frame = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
    }
    else{
        _videoPlayManageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    [_videoPlayManageView enterInFullScreen];
}
- (void)rotateToLandscapeLeft{
    if(_lastOrientationValue==UIInterfaceOrientationLandscapeLeft){
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        _videoPlayManageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
        [self rotateToFullScreen];
    } completion:^(BOOL finished) {

    }];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
    _lastOrientationValue = UIInterfaceOrientationLandscapeLeft;
}
- (void)rotateToLandscapeRight{
    if(_lastOrientationValue==UIInterfaceOrientationLandscapeRight){
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        _videoPlayManageView.transform = CGAffineTransformMakeRotation(M_PI/2);
        [self rotateToFullScreen];
    } completion:^(BOOL finished) {

    }];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
    _lastOrientationValue = UIInterfaceOrientationLandscapeRight;
}
- (void)rotateToPortrait{
    if(_lastOrientationValue==UIInterfaceOrientationPortrait){
        return;
    }
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [UIView animateWithDuration:0.3 animations:^{
        _videoPlayManageView.transform = CGAffineTransformIdentity;
        _videoPlayManageView.frame = _normalScreenFrame;
        [_videoPlayManageView exitFromFullScreen];
    } completion:^(BOOL finished) {

    }];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    _lastOrientationValue = UIInterfaceOrientationPortrait;
}

最后,如果想强制播放器横屏或者竖屏播放,可以这样

- (void)videoPlayManageViewExitFullScreenButtonTapped{
    [self rotateToPortrait];
}
- (void)videoPlayManageViewEnterFullScreenButtonTapped{
    [self rotateToLandscapeRight];
}

OK,大功告成。

PS:demo完整版下载地址为:

https://github.com/wangqi211/VideoPlayDemo.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值