iOS开发视频播放自动横屏

释放双眼,带上耳机,听听看~!

一、通过这个方法改变状态栏方向,枚举设置状态栏方向

 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

需要在viewController中重写

- (BOOL)shouldAutorotate
{
return YES;
}

注意:返回值为YES。

如果此viewController在某个CustomNavigationController中需要在AppDelegate中写个类目

//添加导航栏的扩展
@implementation CustomNavigationController (Rotation)
- (BOOL)shouldAutorotate
{
//在viewControllers中返回需要改变的viewController
for (UIViewController *vc in self.viewControllers) {
if ([vc isKindOfClass:[XXXXViewController class]]) {
return [vc shouldAutorotate];
}
}
return NO;
}
@end

如果此CustomNavigationController在某个CusTabbarViewController中需要在AppDelegate中再写个类目

//添加Tabbar的扩展
@implementation CusTabbarViewController (Rotation)
- (BOOL)shouldAutorotate
{
//在viewControllers中返回需要改变的viewController
for (UIViewController *vc in self.viewControllers) {
if ([vc isKindOfClass:[XFPlayerViewController class]]) {
return [vc shouldAutorotate];
}
}
return NO;
}
@end

二、iOS8 横屏状态栏不显示解决方法:
使用完上面的方法之后发现状态栏确实是改变方向了,但是状态栏不显示出来~
1:在plist文件中将 View controller-based status bar appearance 设置为NO

 

plist文件添加设置.png

2:还需要application:didFinishLaunchingWithOptions:中添加以下下面代码(下面的两段代码必不可少)

AppDelegate.png
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

注意:横屏返回后,会出现页面整体上移的问题。解决方案如下:
对状态栏和导航栏同事做改动的时候,先改变状态栏的变化,然后在改变导航栏的变化。

//切记先改变状态栏的变化,后改变导航栏的变化。防止页面上移
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
self.navigationController.navigationBar.hidden = YES;
self.navigationController.navigationBar.translucent = YES;

如果没有效果,可以试试在plist文件中添加:
Status bar is initially hidden=YES 和
View controller-based status bar appearance= NO

 

plist.png

三、监听设备方向,自动横屏播放(需要关闭设备竖屏锁定)
1.在View或者ViewController里面监听设备方向更改的通知方法
下面示例是在View里面监听的通知:

//旋转屏幕通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
//在delloc里面移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
#pragma mark - 监听设备方向和全屏
- (void)onDeviceOrientationChange
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;
switch (interfaceOrientation) {
case UIInterfaceOrientationPortraitUpsideDown:{
NSLog(@"第3个旋转方向---电池栏在下");
}
break;
case UIInterfaceOrientationPortrait:{
NSLog(@"第0个旋转方向---电池栏在上");
if (!self.toolsView.isHalfScreen) {
NSLog(@"大屏");
[self screenRotatingIdentity];
} else {
NSLog(@"小屏");
}
}
break;
case UIInterfaceOrientationLandscapeLeft:{
NSLog(@"第2个旋转方向---电池栏在左");
[self screenRotatingWithDirection:interfaceOrientation];
}
break;
case UIInterfaceOrientationLandscapeRight:{
NSLog(@"第1个旋转方向---电池栏在右");
[self screenRotatingWithDirection:interfaceOrientation];
}
break;
default:
break;
}
}
//左右、上下旋转屏幕
- (void)screenRotatingWithDirection:(UIInterfaceOrientation)direction
{
_toolsView.isHalfScreen = NO;
CGAffineTransform transform = direction == UIInterfaceOrientationLandscapeLeft ? CGAffineTransformMakeRotation(-(M_PI / 2)) : CGAffineTransformMakeRotation(M_PI / 2);
[UIView animateWithDuration:0.3 animations:^{
self.transform = transform;
} completion:nil];
self.frame = self.bigFrame;
//记录上次设备方向 (在控制器里面更改方向有问题时使用)
if (_lastDevDir == UIInterfaceOrientationLandscapeLeft || _lastDevDir == UIInterfaceOrientationLandscapeRight) {
_videoController.view.frame = CGRectMake(0.f, 0.f, DEVICE_HEIGHT, DEVICE_WIDTH);
}
_lastDevDir = interfaceOrientation;
//此处一定要先改变状态栏再改变导航栏,否则页面上移
[[UIApplication sharedApplication] setStatusBarOrientation:direction animated:YES];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
[GlobalTool sharedTool].currentViewController.navigationController.navigationBar.hidden = YES;
[GlobalTool sharedTool].currentViewController.navigationController.navigationBar.translucent = YES;
}
//屏幕还原
- (void)screenRotatingIdentity
{
[self.toolsView hideOperationView:NO];
_toolsView.isHalfScreen = YES;
[UIView animateWithDuration:0.3 animations:^{
self.transform = CGAffineTransformIdentity;
} completion:nil];
//记录上次设备方向 (在控制器里面更改方向有问题时使用)
// _lastDevDir = UIInterfaceOrientationPortrait;
self.frame = self.smallFrame;
//此处一定要先改变状态栏再改变导航栏,否则页面上移
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
[GlobalTool sharedTool].currentViewController.navigationController.navigationBar.hidden = NO;
[GlobalTool sharedTool].currentViewController.navigationController.navigationBar.translucent = NO;
}

慢慢来,一步一个巴掌印。。。。。

人已赞赏
iOS文章

IOS客户端如何接入微信支付的教程

2020-2-14 12:22:23

iOS文章

iOS 仿阿里巴巴/电商规格选择器 电商通用规格选择器。

2020-2-14 12:44:44

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索