iOS 视频播放横竖屏的转化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30963589/article/details/83340521

我们在做视频播放开发的时候,常常会用到横屏的播放,今天就记录下我做项目时候做横屏的方法,主要是使用注册通知,然后识别手机是否旋转方向去旋转横屏。
思路:
获取到手机已经旋转的通知以后,先改变播放器的transform进行旋转,然后改变frame 去改变播放器的大小。
核心代码如下:

/** 非全屏状态下播放器 superview */
@property (nonatomic, strong) UIView *originalSuperview;
/** 非全屏状态下播放器 frame 作为中间值用来记录原来的frame */
@property (nonatomic, assign) CGRect originalRect;
/** 是否是全屏状态*/
@property (nonatomic) BOOL isfullScreen;


/* 监听屏幕旋转   注册通知*/
- (void)screenRotationNotification{
    //监听屏幕旋转
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)setIsfullScreen:(BOOL)isfullScreen{
    _isfullScreen = isfullScreen;
//    self.controlPlayer.IsfullScreen = _isfullScreen;
}
/** 屏幕翻转监听事件 */
- (void)orientationChanged:(NSNotification *)notify
{
    [self orientationAspect];
}
/** 根据屏幕旋转方向改变当前视频屏幕状态 */
- (void)orientationAspect
{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationLandscapeLeft){
        if (!_isfullScreen){
            [self videoZoomInWithDirection:UIInterfaceOrientationLandscapeRight];
        }
    }
    else if (orientation == UIDeviceOrientationLandscapeRight){
        if (!_isfullScreen){
            [self videoZoomInWithDirection:UIInterfaceOrientationLandscapeLeft];
        }
    }
    else if(orientation == UIDeviceOrientationPortrait){
        if (_isfullScreen){
            [self videoZoomOut];
        }
    }
}
/**
 视频放大全屏幕
 @param orientation 旋转方向
 */
- (void)videoZoomInWithDirection:(UIInterfaceOrientation)orientation
{
    _originalSuperview = self.superview;
    _originalRect = self.frame;

/* 下面的方法是把播放器添加到keyWindow上  这样便于转化    但是有个问题就是会覆盖播放器上面的控制器视图,横屏展示时会变为第一视图,甚至会盖住状态栏

我们可以根据isfullScreen 去写个代理 ,去控制器界面改变播放器的frame来解决*/
//    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
//    [keyWindow addSubview:self];
    
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    //    [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO];
    [UIView animateWithDuration:duration animations:^{
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(screenRotationChange:)]) {
            [self.delegate screenRotationChange:YES];
        }
        if (orientation == UIInterfaceOrientationLandscapeLeft){
            self.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }else if (orientation == UIInterfaceOrientationLandscapeRight) {
            self.transform = CGAffineTransformMakeRotation(M_PI/2);
        }
    }completion:^(BOOL finished) {}];
    
    self.frame = [UIScreen mainScreen].bounds;
    [self setNeedsLayout];
    [self layoutIfNeeded];
    self.isfullScreen = YES;
    
}

// 视频退出全屏幕
- (void)videoZoomOut
{
    //退出全屏时强制取消隐藏状态栏
    //    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    //    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    [UIView animateWithDuration:duration animations:^{
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(screenRotationChange:)]) {
            [self.delegate screenRotationChange:NO];
        }
        self.transform = CGAffineTransformMakeRotation(0);
    }completion:^(BOOL finished) {
        
    }];
    self.frame = _originalRect;
    [_originalSuperview addSubview:self];
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    self.isfullScreen = NO;
}

猜你喜欢

转载自blog.csdn.net/qq_30963589/article/details/83340521