iOS 视频播放横竖屏的转化

7 篇文章 0 订阅

我们在做视频播放开发的时候,常常会用到横屏的播放,今天就记录下我做项目时候做横屏的方法,主要是使用注册通知,然后识别手机是否旋转方向去旋转横屏。
思路:
获取到手机已经旋转的通知以后,先改变播放器的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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值