视图旋转 AVPlayer视频播放视图旋转
来源:互联网 发布:域名注册申请 编辑:程序博客网 时间:2024/06/03 23:45
/*
当在做视频播放器的时候,需要对播放视频的那个view进行旋转操作,当控制中心(上拉的那个菜单)开启旋转屏幕,点击全屏按钮或把手机横屏视图都会全屏,全屏状态下,点击退出全屏按钮或将屏幕旋转到正方向,视图都会退出全屏;如果控制中心关闭旋转屏幕,旋转屏幕不再能用,但进入和退出全屏按钮都是能用的
在这期间,状态条也是要改变方向的,而且是一直显示的,现在专业做视频的APP都是这样的,下面是怎样实现的屏幕旋转的这一块:
1. 方案1 :强制旋转屏幕 + SizeClasses这个需要调用私有API(审核可能被拒绝),而且现在私有API有的也不能用了,无法强制退出横屏,所以这个方案不可以;
2. 方案2 :这就是我即将说的这个,主流视频APP都是这么做的,一开始我做播放器的时候先就差这一点没有解决,蛋疼了好几天,现在终于全都解决了,以后可能会发AVPlayer视频播放的博客,包括音量 . 亮度.手势等等, 不过下一篇我还是会总结一些旋转屏幕时遇到的各种问题;
当在做视频播放器的时候,需要对播放视频的那个view进行旋转操作,当控制中心(上拉的那个菜单)开启旋转屏幕,点击全屏按钮或把手机横屏视图都会全屏,全屏状态下,点击退出全屏按钮或将屏幕旋转到正方向,视图都会退出全屏;如果控制中心关闭旋转屏幕,旋转屏幕不再能用,但进入和退出全屏按钮都是能用的
在这期间,状态条也是要改变方向的,而且是一直显示的,现在专业做视频的APP都是这样的,下面是怎样实现的屏幕旋转的这一块:
1. 方案1 :强制旋转屏幕 + SizeClasses这个需要调用私有API(审核可能被拒绝),而且现在私有API有的也不能用了,无法强制退出横屏,所以这个方案不可以;
2. 方案2 :这就是我即将说的这个,主流视频APP都是这么做的,一开始我做播放器的时候先就差这一点没有解决,蛋疼了好几天,现在终于全都解决了,以后可能会发AVPlayer视频播放的博客,包括音量 . 亮度.手势等等, 不过下一篇我还是会总结一些旋转屏幕时遇到的各种问题;
(首先确定你的程序info.plist添加了要旋转的方向)
*/
@interfaceLRLAVPlayerController ()
@end
@implementation LRLAVPlayerController
//懒加载的方式来获取一个默认的model,用来模拟视频的尺寸;
-(AVPlayerModel *)avModel{
if (!_avModel) {
_avModel = [[AVPlayerModelalloc]init];
_avModel.videoSize = CGSizeMake(SCREEN_WIDTH,200);
}
return_avModel;
}
//实例化,一个模拟播放视频的view
-(void)initAVPlayerView{
self.avplayerView = [[NSBundlemainBundle]loadNibNamed:@"LRLAVPlayerView"owner:selfoptions:nil].lastObject;
[self.viewaddSubview:self.avplayerView];
self.videoHeight = SCREEN_WIDTH * (self.avModel.videoSize.height/self.avModel.videoSize.width);
//使用masonry给视图加限制(autoLayout)
[self.avplayerViewmas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(40);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.height.equalTo(@(self.videoHeight));
}];
}
//点击进入全屏按钮
- (IBAction)toLaunch:(id)sender {
[selftoOrientation:UIInterfaceOrientationLandscapeLeft];
}
//点击退出全屏按钮
- (IBAction)toPra:(id)sender {
[selftoOrientation:UIInterfaceOrientationPortrait];
}
//点击进入,退出全屏,或者监测到屏幕旋转去调用的方法
-(void)toOrientation:(UIInterfaceOrientation)orientation{
//获取到当前状态条的方向
UIInterfaceOrientation currentOrientation = [UIApplicationsharedApplication].statusBarOrientation;
//判断如果当前方向和要旋转的方向一致,那么不做任何操作
if (currentOrientation == orientation) {
return;
}
//根据要旋转的方向,使用Masonry重新修改限制
if (orientation ==UIInterfaceOrientationPortrait) {
[self.avplayerViewmas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(40);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.height.equalTo(@(self.videoHeight));
}];
}else{
//这个地方加判断是为了从全屏的一侧,直接到全屏的另一侧不用修改限制,否则会出错;
if (currentOrientation ==UIInterfaceOrientationPortrait) {
[self.avplayerViewmas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(SCREEN_HEIGHT));
make.height.equalTo(@(SCREEN_WIDTH));
make.center.equalTo(self.avplayerView.superview);
}];
}
}
//iOS6.0之后,设置状态条的方法能使用的前提是shouldAutorotate为NO,也就是说这个视图控制器内,旋转要关掉;
//也就是说在实现这个方法的时候-(BOOL)shouldAutorotate返回值要为NO
[[UIApplicationsharedApplication]setStatusBarOrientation:orientationanimated:NO];
//获取旋转状态条需要的时间:
CGFloat duration = [UIApplicationsharedApplication].statusBarOrientation;
[UIViewbeginAnimations:nilcontext:nil];
//更改了状态条的方向,但是设备方向UIInterfaceOrientation还是正方向的,这就要设置给你播放视频的视图的方向设置旋转
//给你的播放视频的view视图设置旋转
self.avplayerView.transform = [selfgetOrientation];
[UIViewsetAnimationDuration:duration];
//开始旋转
[UIViewcommitAnimations];
}
//根据想要旋转的方向来设置旋转
-(CGAffineTransform)getOrientation{
//状态条的方向已经设置过,所以这个就是你想要旋转的方向
UIInterfaceOrientation orientation = [UIApplicationsharedApplication].statusBarOrientation;
//根据要进行旋转的方向来计算旋转的角度
if (orientation ==UIInterfaceOrientationPortrait) {
returnCGAffineTransformIdentity;
}elseif (orientation ==UIInterfaceOrientationLandscapeLeft){
returnCGAffineTransformMakeRotation(-M_PI_2);
}elseif(orientation ==UIInterfaceOrientationLandscapeRight){
returnCGAffineTransformMakeRotation(M_PI_2);
}
returnCGAffineTransformIdentity;
}
//视图控制器实现的方法
-(BOOL)shouldAutorotate{ //iOS6.0之后,要想让状态条可以旋转,必须设置视图不能自动旋转
returnNO;
}
//这就遇到一个问题,可以手动进行设置旋转的方向,但我们关掉了自动旋转的开关,那么如果我想要既可以手动操作又想自动旋转呢,可现在我已经把自动旋转关了呀,别急,系统提供了一个通知,即使不开启旋转也能监测到旋转
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
//实例化播放视频的视图,实现在最上边
[selfinitAVPlayerView];
//获取设备旋转方向的通知,即使关闭了自动旋转,一样可以监测到设备的旋转方向
[[UIDevicecurrentDevice]beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotificationobject:nil];
}
//获取设备的旋转方向,然后进行判断如何旋转UI
-(void)orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevicecurrentDevice]orientation];
//根据监测到的旋转方向,进行旋转
switch (orientation) {
caseUIDeviceOrientationPortrait:
[selftoOrientation:UIInterfaceOrientationPortrait];
break;
caseUIDeviceOrientationLandscapeLeft:
//UIDeviceOrientation和 UIInterfaceOrientation在左右方向上是翻着的
[selftoOrientation:UIInterfaceOrientationLandscapeRight];
break;
caseUIDeviceOrientationLandscapeRight:
[selftoOrientation:UIInterfaceOrientationLandscapeLeft];
break;
default:
break;
}
}
*/
@interfaceLRLAVPlayerController ()
//用来播放视频的view
@property(nonatomic,strong)LRLAVPlayerView* avplayerView;
//用来存储这个view的尺寸, 为了模拟存储视频信息的的model
@property(nonatomic,strong)AVPlayerModel* avModel;
//view的高度
@property(nonatomic,assign)CGFloatvideoHeight;
@end
@implementation LRLAVPlayerController
//懒加载的方式来获取一个默认的model,用来模拟视频的尺寸;
-(AVPlayerModel *)avModel{
if (!_avModel) {
_avModel = [[AVPlayerModelalloc]init];
_avModel.videoSize = CGSizeMake(SCREEN_WIDTH,200);
}
return_avModel;
}
//实例化,一个模拟播放视频的view
-(void)initAVPlayerView{
self.avplayerView = [[NSBundlemainBundle]loadNibNamed:@"LRLAVPlayerView"owner:selfoptions:nil].lastObject;
[self.viewaddSubview:self.avplayerView];
self.videoHeight = SCREEN_WIDTH * (self.avModel.videoSize.height/self.avModel.videoSize.width);
//使用masonry给视图加限制(autoLayout)
[self.avplayerViewmas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(40);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.height.equalTo(@(self.videoHeight));
}];
}
//点击进入全屏按钮
- (IBAction)toLaunch:(id)sender {
[selftoOrientation:UIInterfaceOrientationLandscapeLeft];
}
//点击退出全屏按钮
- (IBAction)toPra:(id)sender {
[selftoOrientation:UIInterfaceOrientationPortrait];
}
//点击进入,退出全屏,或者监测到屏幕旋转去调用的方法
-(void)toOrientation:(UIInterfaceOrientation)orientation{
//获取到当前状态条的方向
UIInterfaceOrientation currentOrientation = [UIApplicationsharedApplication].statusBarOrientation;
//判断如果当前方向和要旋转的方向一致,那么不做任何操作
if (currentOrientation == orientation) {
return;
}
//根据要旋转的方向,使用Masonry重新修改限制
if (orientation ==UIInterfaceOrientationPortrait) {
[self.avplayerViewmas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(40);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.height.equalTo(@(self.videoHeight));
}];
}else{
//这个地方加判断是为了从全屏的一侧,直接到全屏的另一侧不用修改限制,否则会出错;
if (currentOrientation ==UIInterfaceOrientationPortrait) {
[self.avplayerViewmas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(SCREEN_HEIGHT));
make.height.equalTo(@(SCREEN_WIDTH));
make.center.equalTo(self.avplayerView.superview);
}];
}
}
//iOS6.0之后,设置状态条的方法能使用的前提是shouldAutorotate为NO,也就是说这个视图控制器内,旋转要关掉;
//也就是说在实现这个方法的时候-(BOOL)shouldAutorotate返回值要为NO
[[UIApplicationsharedApplication]setStatusBarOrientation:orientationanimated:NO];
//获取旋转状态条需要的时间:
CGFloat duration = [UIApplicationsharedApplication].statusBarOrientation;
[UIViewbeginAnimations:nilcontext:nil];
//更改了状态条的方向,但是设备方向UIInterfaceOrientation还是正方向的,这就要设置给你播放视频的视图的方向设置旋转
//给你的播放视频的view视图设置旋转
self.avplayerView.transform = [selfgetOrientation];
[UIViewsetAnimationDuration:duration];
//开始旋转
[UIViewcommitAnimations];
}
//根据想要旋转的方向来设置旋转
-(CGAffineTransform)getOrientation{
//状态条的方向已经设置过,所以这个就是你想要旋转的方向
UIInterfaceOrientation orientation = [UIApplicationsharedApplication].statusBarOrientation;
//根据要进行旋转的方向来计算旋转的角度
if (orientation ==UIInterfaceOrientationPortrait) {
returnCGAffineTransformIdentity;
}elseif (orientation ==UIInterfaceOrientationLandscapeLeft){
returnCGAffineTransformMakeRotation(-M_PI_2);
}elseif(orientation ==UIInterfaceOrientationLandscapeRight){
returnCGAffineTransformMakeRotation(M_PI_2);
}
returnCGAffineTransformIdentity;
}
//视图控制器实现的方法
-(BOOL)shouldAutorotate{ //iOS6.0之后,要想让状态条可以旋转,必须设置视图不能自动旋转
returnNO;
}
//这就遇到一个问题,可以手动进行设置旋转的方向,但我们关掉了自动旋转的开关,那么如果我想要既可以手动操作又想自动旋转呢,可现在我已经把自动旋转关了呀,别急,系统提供了一个通知,即使不开启旋转也能监测到旋转
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
//实例化播放视频的视图,实现在最上边
[selfinitAVPlayerView];
//获取设备旋转方向的通知,即使关闭了自动旋转,一样可以监测到设备的旋转方向
[[UIDevicecurrentDevice]beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotificationobject:nil];
}
//获取设备的旋转方向,然后进行判断如何旋转UI
-(void)orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevicecurrentDevice]orientation];
//根据监测到的旋转方向,进行旋转
switch (orientation) {
caseUIDeviceOrientationPortrait:
[selftoOrientation:UIInterfaceOrientationPortrait];
break;
caseUIDeviceOrientationLandscapeLeft:
//UIDeviceOrientation和 UIInterfaceOrientation在左右方向上是翻着的
[selftoOrientation:UIInterfaceOrientationLandscapeRight];
break;
caseUIDeviceOrientationLandscapeRight:
[selftoOrientation:UIInterfaceOrientationLandscapeLeft];
break;
default:
break;
}
}
@end
0 0
- 视图旋转 AVPlayer视频播放视图旋转
- 视图旋转 AVPlayer视频播放视图旋转
- 再谈iOS视频播放器旋转视图开发
- 旋转视图
- 视图的Transform旋转
- 旋转(两个视图)
- 视图旋转的宏
- 横屏竖屏,视图旋转
- 关于视图的旋转
- UI旋转变换视图
- 旋转和缩放视图
- 贝塞尔曲线 旋转视图
- iOS旋转视图实践
- 视频播放屏幕旋转
- ios旋转视图 视图动画效果CGAffineTransformRotate
- 对视图旋转360度
- Android之旋转button视图
- 如何旋转工程图的视图
- using libnet to send igmp join packet.
- Python——4Dict和Set类型
- Codeforces Round #309 (Div. 2) E DFS 染色 + 快速乘
- 算法实战(三)--求K-Pairs
- NPOI操作excel异常
- 视图旋转 AVPlayer视频播放视图旋转
- 改变image的尺寸大小,按原比例不拉伸。
- 大话行车记录器产品系统设计 - 基于芯片商提供的SDK API开发之三
- 使用matplotlib库绘图出现的问题
- 递归再谈
- HDU1756:Cupid's Arrow
- mac下Genymotion添加虚拟设备提示 The file is corrupt. Do you want to download the file again?解决方法
- The Definitive Guide to Using Negative Margins
- 直接使用默认设置进行打印:(2)使用MFC编程实现打印中文字符串