关于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, 比较好用的翻墙

0 0
原创粉丝点击