最近一直在作视频相关的项目,其中一个很重要的功能就是播放时的横竖屏切换,因而研究对比了下市场上主要视频类APP的横竖屏切换方式,共分为两种,一种以优酷视频和土豆视频为表明,当横放手机时整个界面都旋转了,另外一种以腾讯视频,搜狐视频为表明,当横放手机时只是播放的小视图旋转,其他内容不变。实现方法分别以下。git
1 优酷视频的横竖屏切换的实现方法github
首先控制整个viewcontroller的view支持横竖屏切换的几个方法web
- (BOOL)shouldAutorotate{ return YES;
}
- (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationPortrait;
}
而后添加监测屏幕方向变化的通知ruby
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationHasChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
接着在通知的回调方法中操做以下app
- (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_WIDTH, SCREEN_HEIGHT);
}
else{
_videoPlayManageView.frame = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH);
}
[_videoPlayManageView enterInFullScreen];
}
- (void)rotateToLandscapeLeft{
if(_lastOrientationValue==UIInterfaceOrientationLandscapeLeft){
return;
}
[self rotateToFullScreen];
_lastOrientationValue = UIInterfaceOrientationLandscapeLeft;
}
- (void)rotateToLandscapeRight{
if(_lastOrientationValue==UIInterfaceOrientationLandscapeRight){
return;
}
[self rotateToFullScreen];
_lastOrientationValue = UIInterfaceOrientationLandscapeRight;
}
- (void)rotateToPortrait
{
if(_lastOrientationValue==UIInterfaceOrientationPortrait){
return;
}
_videoPlayManageView.frame = _normalScreenFrame;
[_videoPlayManageView exitFromFullScreen];
_lastOrientationValue = UIInterfaceOrientationPortrait;
}
最后,若是想强制播放器横屏或者竖屏播放,能够这样ide
- (void)videoPlayManageViewExitFullScreenButtonTapped{
if([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]){ SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
[UIViewController attemptRotationToDeviceOrientation];
}
- (void)videoPlayManageViewEnterFullScreenButtonTapped{
if([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]){ SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
[UIViewController attemptRotationToDeviceOrientation];
[self rotateToLandscapeRight];
}
OK,大功告成。svg
PS:demo完整版下载地址为:ui