关于iOS横竖屏幕强制转换的问题[强制横屏]
来源:互联网 发布:linux cp文件到桌面 编辑:程序博客网 时间:2024/06/05 18:46
关于iOS横竖屏幕强制转换的问题[强制横屏]
注: 感谢开发者”张记”, 开发者”diogo-appdev” 提供的思路
关于网上流传的”两种方案”其实并不适合大多数开发者的业务逻辑和需求所以暂不讨论
众所周知网上流传着两种解决横竖屏幕的方案分别是设置transform和performSelector, transform需要处理的东西首先如果您的应用只是分散的单独界面需要适配的话, 处理的东西过于繁杂, 例如我现在的所做的应用需要处理广告界面, 视屏界面, 以及PPT课件界面的横竖屏幕处理, 其他界面都不需要. 考虑到我自己很懒而且讨厌UIBug的缘故与大家分享一下横竖屏幕的smartPoint
都知道我们强制调用私有API会有什么严重的后果. SO直接上代码以PPT课件界面举例, 效果图如下:
我们在网上常见的方法:
[1].
if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) { //注意用@()包装一下枚举, 否则会报错 [[UIDevice currentDevice]performSelector:@selector(setOrientation:) withObject:@(UIInterfaceOrientationLandscapeLeft)];}
[2].
//竖 -> 横//这句话是防止手动先把设备置为横屏[设备已经倾斜],导致下面的语句失效.[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];//横 -> 竖//这句话是防止手动先把设备置为竖屏,导致下面的语句失效.[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
[3].
这种方法是无效的, 至于为什么无效暂时还没搞明白
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);//使用这个方法时先把 Build Setting--> Apple LLVM X.X - Preprocessing--> Enable Strict Checking of objc_msgSend Calls 改为 NO;
首先, 前两种方法都能达到预期的效果, 已经验证, 但是由于我们知道的EggPain的审核机制, 使用私有的API是有被拒绝的风险的. 由于自己的APP一直使用的下面这种方法, 所以也没有提交尝试, 尝试过的Developer可以分享一下到底是”有风险”、”有很大风险”还是”几率问题”.
言归正传, 这是目前我们在采用的方法, 审核是没有问题的
首先我们在viewController中设置:
#warning – 这个设置不是必需的, 根据也无需要在基类或者单独设置都可以
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{ //return YES; return UIInterfaceOrientationMaskPortrait;}- (BOOL)shouldAutorotate{ return YES;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);}
然后由于我们写个自定义方法即可:
NSInvocation 的具体用法请大家自行百度.
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{ // arc下 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 = orientation; [invocation setArgument:&val atIndex:2]; [invocation invoke]; }}
点击横屏竖屏按钮的时候调用传入参数就可以了. 总之还是很简单的.
希望编程世界越来越美好.
另附: http://www.2-vpn4.cc/home.action?ic=8BA9B63D14 二师兄VPN, 比较好用的翻墙
- 关于iOS横竖屏幕强制转换的问题[强制横屏]
- IOS强制屏幕横竖屏相互切换
- iOS屏幕旋转,强制横竖屏
- iOS 强制横竖屏
- iOS强制横竖屏
- iOS 部分页面的强制横竖屏
- IOS强制横竖屏幕切换总结续
- IOS强制横竖屏切换
- iOS强制转换横竖屏和键盘方向控制
- 关于iOS强制横屏
- 关于强制类型转换的小问题
- iOS 关于项目中某个界面需要横竖屏切换或者强制横屏
- 关于强制横竖屏的几点说明
- Blackberry强制横竖屏
- Android 强制横竖屏
- Android强制横竖屏
- ios8 强制横竖屏
- 关于C++的强制转换
- 提高项目30.2-字符串处理函数 指针形参
- JDK JRE 的作用与区别【转】
- 使用JAVA读写Properties属性文件
- 操作系统
- 程序员永远不要再犯的5个编程bug
- 关于iOS横竖屏幕强制转换的问题[强制横屏]
- iOS中 如何将自己的框架更新到cocopods上
- 创业的真谛是顺势而为,借船过海!
- 消息队列
- JAVA学习记录(一)——Number\Character\String\数组\Date\Calendar\GregorianCalendar
- 为什么我们这么期待Kubernetes?
- 选了3个短信平台进行对比
- charCount.js实现输入文字限制
- 关于Win7 64位下:Navicat无法连接64位Oracle 11gR2:Cannot load OCI DLL 87 解决方法