1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > ios键盘横屏_iOS 强制横屏 部分横屏等功能实践

ios键盘横屏_iOS 强制横屏 部分横屏等功能实践

时间:2019-07-08 13:03:07

相关推荐

ios键盘横屏_iOS 强制横屏 部分横屏等功能实践

需求如下:

1.app整体只能竖屏,部分页面才可以横屏

2.app整体只能竖屏,部分页面也是竖屏,但是点击某个按钮可以使当前页面变为横屏,如全屏视频播放键。

需求1解决方法:

1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持

屏幕快照 -12-08 下午6.41.26.png

2.在appdelegate的.h文件声明属性来标记当前设备方向@property(assign,nonatomic)UIInterfaceOrientationinterfaceOrientation;

在appdelegate的.m文件中:-(UIInterfaceOrientationMask)application:(UIApplication*)applicationsupportedInterfaceOrientationsForWindow:(nullableUIWindow*)window{

if(self.interfaceOrientation==UIInterfaceOrientationUnknown){//直播间用

returnUIInterfaceOrientationMaskAllButUpsideDown;

}elseif(self.interfaceOrientation==UIInterfaceOrientationLandscapeRight){

//交易页面横屏用

returnUIInterfaceOrientationMaskLandscapeRight;

}else{returnUIInterfaceOrientationMaskPortrait;

}

}

3.在需要横屏的ViewController的ViewWillApper方法里面:Appdelegate*delegate=p.p1[UIApplicationsharedApplication].delegate;

delegate.canRotate=YES;

需求2解决方法:

方法1:

1.在targets - general中设置设备支持方向如下,确保整体都是竖屏

屏幕快照 -12-08 下午6.30.25.png

2.在横屏(如button点击)事件处,代码如下-(void)fullScreenBtnAction{if(_isNormalOrientation){//如果当前是默认的竖屏

//注:当前self是UIView对象,如果是VC,则为self.view.....

self.frame=CGRectMake(0,0,kScreenSize.height,kScreenSize.width);CGRectframe=[UIScreenmainScreen].applicationFrame;//transfrom会以当前center为锚点旋转,所以旋转后位置有偏移,需要处理

CGPointcenter=CGPointMake(frame.origin.x+ceil(frame.size.width/2),frame.origin.y+ceil(frame.size.height/2));self.center=center;

//取状态栏旋转时间//CGFloatduration=[UIApplicationsharedApplication].statusBarOrientationAnimationDuration;

[UIViewbeginAnimations:nilcontext:nil];

[UIViewsetAnimationDuration:0.3];self.transform=CGAffineTransformMakeRotation(M_PI_2);

[UIViewcommitAnimations];

}else{

[UIViewbeginAnimations:nilcontext:nil];

[UIViewsetAnimationDuration:0.3];self.transform=CGAffineTransformIdentity;

[UIViewcommitAnimations];//_originRect是进入横屏前self的frame

self.frame=_originRect;

}

_isNormalOrientation=!_isNormalOrientation;//更改状态}

***方法2:

1.在targets - general中设置设备支持方向如下,确保设备各个方法均支持 (弃用 可不设置)

屏幕快照 -12-08 下午6.41.26.png

2.在appdelegate的.h文件声明属性interfaceOrientation标记方向@property(assign,nonatomic)UIInterfaceOrientationinterfaceOrientation;

在appdelegate的.m文件中实现设备方向方法:-(UIInterfaceOrientationMask)application:(UIApplication*)applicationsupportedInterfaceOrientationsForWindow:(nullableUIWindow*)window{

if(self.interfaceOrientation==UIInterfaceOrientationUnknown){//直播间用

returnUIInterfaceOrientationMaskAllButUpsideDown;

}elseif(self.interfaceOrientation==UIInterfaceOrientationLandscapeRight){//交易页面横屏用

returnUIInterfaceOrientationMaskLandscapeRight;

}else{returnUIInterfaceOrientationMaskPortrait;

}

}横竖屏转换://转换到竖屏-(void)rotateToPortraitScreenWithInvocation{

AppDelegate*delegate=(AppDelegate*)[UIApplicationsharedApplication].delegate;

SELselector=NSSelectorFromString(@"setOrientation:");

NSInvocation*invocation=[NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];

[invocationsetSelector:selector];

[invocationsetTarget:[UIDevicecurrentDevice]];

intval=UIDeviceOrientationPortrait;

[invocationsetArgument:&valatIndex:2];

[invocationinvoke];

delegate.interfaceOrientation=UIInterfaceOrientationPortrait;

}//转换到横屏模式-(void)rotateToLandscapWithIncovation{

AppDelegate*delegate=(AppDelegate*)[UIApplicationsharedApplication].delegate;

delegate.interfaceOrientation=UIInterfaceOrientationUnknown;

SELselector=NSSelectorFromString(@"setOrientation:");

NSInvocation*invocation=[NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];

[invocationsetSelector:selector];

[invocationsetTarget:[UIDevicecurrentDevice]];

intval=UIInterfaceOrientationLandscapeRight;

[invocationsetArgument:&valatIndex:2];

[invocationinvoke];

}

作者:CoderYZY

链接:/p/95cdf3262bc6

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。