咱们在作视频播放开发的时候,经常会用到横屏的播放,今天就记录下我作项目时候作横屏的方法,主要是使用注册通知,而后识别手机是否旋转方向去旋转横屏。
思路:
获取到手机已经旋转的通知之后,先改变播放器的transform进行旋转,而后改变frame 去改变播放器的大小。
核心代码以下:web
/** 非全屏状态下播放器 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; }