iOS视频播放横竖屏切换技巧
ios视频播放横竖屏切换技巧。
一、需求:横竖屏切换。
二、效果:
三、实现:
如上图,点击工具栏的第四个按钮进行横屏切换:
- (void)tooltabbuttonpressed:(akvideotooltab1 *)sender{ if (sender.selectedindex == 4) {//切换至全屏 appdelegate *appdelegate = (appdelegate *)[uiapplication sharedapplication].delegate; appdelegate.allowrotation = yes; [[uiapplication sharedapplication] setstatusbarhidden:yes withanimation:uistatusbaranimationnone];
nsnumber *orientationtarget = [nsnumber numberwithint:uiinterfaceorientationlandscaperight]; [[uidevice currentdevice] setvalue:orientationtarget forkey:@"orientation"];}}
假如在项目配置的时候整个项目是仅支持竖屏,所以在需要横屏的时候就需要通过代码进行控制:
appdelegate.allowrotation = yes;//允许横屏,当然退出本界面的时候要记得将其设为no。然后,设置横屏效果,才会生效:
nsnumber *orientationtarget = [nsnumber numberwithint:uiinterfaceorientationlandscaperight]; [[uidevice currentdevice] setvalue:orientationtarget forkey:@"orientation"];至此,还没真正看得到横屏的效果,需对横屏变化进行监听,然后对ui进行变换。
四、横竖屏通知监听:
ios横竖屏通知有两种,一种设备横竖屏状态变化,另一种状态栏横竖屏状态变化。与ui布局相关的是后者,因为如果一个viewcontroller不支持自动旋转,并且相关view也没有进行横竖屏切换控制,而当设备由竖屏转横屏时依然会执行监听设备横竖屏的通知方法。以下对两种方式的简述:
1.监听设备横竖屏(uideviceorientationdidchangenotification)
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(orientchange:) name:uideviceorientationdidchangenotification object:nil];
4种状态:
uideviceorientationportrait
uideviceorientationportraitupsidedown
uideviceorientationlandscapeleft
uideviceorientationlandscaperight
2.监听状态栏横竖屏状态(uiapplicationdidchangestatusbarorientationnotification)
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(orientchange:) name:uiapplicationdidchangestatusbarorientationnotification object:nil];
4种状态:
uiinterfaceorientationunknown = uideviceorientationunknown,
uiinterfaceorientationportrait = uideviceorientationportrait,
uiinterfaceorientationportraitupsidedown = uideviceorientationportraitupsidedown,
uiinterfaceorientationlandscapeleft = uideviceorientationlandscaperight,
uiinterfaceorientationlandscaperight = uideviceorientationlandscapeleft
要实现上图效果就要监听状态栏横竖屏状态:
#pragma mark ------------orientchangenotification 监听横竖屏切换-------------- - (void)orientchange:(nsnotification *)notification{ uiinterfaceorientation interfaceoritation = [[uiapplication sharedapplication] statusbarorientation]; if (interfaceoritation == uiinterfaceorientationlandscaperight) { videoview.frame = cgrectmake(0 , 0, kscreenwidth, kscreenheight); self.navigationcontroller.navigationbarhidden = yes; cgfloat buttonw = 44; exitfullscreen = ({ uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; button.frame = cgrectmake(30, 10, buttonw, buttonw); [button setimage:[uiimage imagenamed:@"全屏播放-返回"] forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(exitfullscreen) forcontrolevents:uicontroleventtouchupinside]; button; }); [self.view addsubview:exitfullscreen]; }else if(interfaceoritation == uiinterfaceorientationportrait){ cgfloat verticalh = (kscreenwidth*0.6); videoview.frame = cgrectmake(0, 60, kscreenwidth, verticalh); self.navigationcontroller.navigationbarhidden = no; [exitfullscreen removefromsuperview]; } }
当监听到是横屏uiinterfaceorientationlandscaperight的时候就进行横屏的ui布局。当是向竖屏uiinterfaceorientationportrait改变时进行竖屏的ui回归。效果如上。
viewcontroller的代码如下:
// // lhtviewcontroller.m // cacloud // // created by sure on 2017/8/24. // copyright © 2017年 sure. all rights reserved. // #import "lhviewcontroller.h" #import "akvideotooltab1.h" @interface lhviewcontroller (){ uiimageview *videoview; uibutton *exitfullscreen; } @end @implementation lhviewcontroller{ akvideotooltab1 *aktab; } - (void)viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor whitecolor]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(orientchange:) name:uiapplicationdidchangestatusbarorientationnotification object:nil]; [self addui]; } - (void)viewwillappear:(bool)animated{ [super viewwillappear:animated]; } - (void)viewwilldisappear:(bool)animated{ [super viewwilldisappear:animated]; [[uiapplication sharedapplication] setidletimerdisabled:no] ;//关闭屏幕常亮; appdelegate *appdelegate = (appdelegate *)[uiapplication sharedapplication].delegate; appdelegate.allowrotation = no; [[uiapplication sharedapplication] setstatusbarhidden:no withanimation:uistatusbaranimationnone]; nsnumber *orientationtarget = [nsnumber numberwithint:uiinterfaceorientationportrait]; [[uidevice currentdevice] setvalue:orientationtarget forkey:@"orientation"]; [super viewwilldisappear:animated]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } - (void)addui{ cgfloat verticalh = (kscreenwidth*0.6); videoview = [[uiimageview alloc] initwithframe:cgrectmake(0, 60, kscreenwidth, verticalh)]; videoview.backgroundcolor = [uicolor blackcolor]; [self.view addsubview:videoview]; cgfloat toolh = 30; aktab = [[akvideotooltab1 alloc] initwithframe:cgrectmake(0, cgrectgetmaxy(videoview.frame), kscreenwidth,toolh)]; [self.view addsubview:aktab]; cgfloat btnw = 100; cgfloat gap = (kscreenwidth - 3*100)/4; } #pragma mark ------------akvideotooltab1:播放、声音、清晰度、全屏-------------- - (void)tooltabbuttonpressed:(akvideotooltab1 *)sender{ if (sender.selectedindex == 4) {//切换至全屏 appdelegate *appdelegate = (appdelegate *)[uiapplication sharedapplication].delegate; appdelegate.allowrotation = yes; [[uiapplication sharedapplication] setstatusbarhidden:yes withanimation:uistatusbaranimationnone]; nsnumber *orientationtarget = [nsnumber numberwithint:uiinterfaceorientationlandscaperight]; [[uidevice currentdevice] setvalue:orientationtarget forkey:@"orientation"]; } } #pragma mark ------------orientchangenotification 监听横竖屏切换-------------- - (void)orientchange:(nsnotification *)notification{ uiinterfaceorientation interfaceoritation = [[uiapplication sharedapplication] statusbarorientation]; if (interfaceoritation == uiinterfaceorientationlandscaperight) { videoview.frame = cgrectmake(0 , 0, kscreenwidth, kscreenheight); self.navigationcontroller.navigationbarhidden = yes; cgfloat buttonw = 44; exitfullscreen = ({ uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; button.frame = cgrectmake(30, 10, buttonw, buttonw); [button setimage:[uiimage imagenamed:@"全屏播放-返回"] forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(exitfullscreen) forcontrolevents:uicontroleventtouchupinside]; button; }); [self.view addsubview:exitfullscreen]; }else if(interfaceoritation == uiinterfaceorientationportrait){ cgfloat verticalh = (kscreenwidth*0.6); videoview.frame = cgrectmake(0, 60, kscreenwidth, verticalh); self.navigationcontroller.navigationbarhidden = no; [exitfullscreen removefromsuperview]; } } -(void)exitfullscreen{ appdelegate *appdelegate = (appdelegate *)[uiapplication sharedapplication].delegate; appdelegate.allowrotation = no; [[uiapplication sharedapplication] setstatusbarhidden:no withanimation:uistatusbaranimationnone]; nsnumber *orientationtarget = [nsnumber numberwithint:uiinterfaceorientationportrait]; [[uidevice currentdevice] setvalue:orientationtarget forkey:@"orientation"]; } /* #pragma mark - navigation // in a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // get the new view controller using [segue destinationviewcontroller]. // pass the selected object to the new view controller. } */ @end
上一篇: cad图纸按比例放到布局视口?
下一篇: SAI怎么画椭圆?SAI绘制椭圆技巧