软件测试之性能测试(ios)——屏幕旋转

最近在写一个ios性能SDK,遇到了一个问题,就是屏幕旋转。对于一个通过presentViewController出来的界面,要么竖着进入竖着退出,要么横着进入横着退出。但是在我们项目app应用的结果却是竖着进入,横着退出,造成了unrecognized selector sent to instance类型的crash,crash的原因分析就不详细开展了,是其他原因引起的。

1、定位presentViewController出来的界面竖着进入横着退出的原因

通过查看开发的源代码发现,在rootViewController里面实现了如下方法

- (NSUInteger) supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

- (BOOL) shouldAutorotate {
    return YES;
}

这两个方法表示,该app支持屏幕旋转,但是只支持横屏。这就解释了横着退出的原因了。

2、让presentViewController出来的界面横着进入横着退出
横着退出是rootViewController定义的行为,不好改变。那只能想着怎么把presentViewController出来的界面横着进入。

整体上有如下几个方案:

2.1 在target中设置
【Targets】中设置
在这里插入图片描述
这种方式不行,因为我的SDK不应该改变开发对app的设置

2.2 UIWindow设置

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window NS_AVAILABLE_IOS(6_0);

这种方式不行,因为我的SDK不能为AppDelegate.m中添加任何函数

2.3 UIViewController设置

//Interface的方向是否会跟随设备方向自动旋转,如果返回NO,后两个方法不会再调用
- (BOOL)shouldAutorotate {
    return YES;
}
//返回直接支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
//返回最优先显示的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

这个方式可行,因为可以灵活控制自己SDK的ViewController方向。但是这种方式只有在是window的rootViewController或者modal模式下才生效。否则UIKit并不会执行上述方法。具体解决办法可以参考文章1。

2.4 强制旋转

NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

具体使用可以参考文章2、3、4、5
这种方式也不行,因为也需要在AppDelegate.m中添加方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window

{
    
    if (self.allowRotation == YES) {
        //横屏
        return UIInterfaceOrientationMaskLandscape;
        
    }else{
        //竖屏
        return UIInterfaceOrientationMaskPortrait;
        
    }
    
}

3、兼容横屏是横着进横着出,竖屏是竖着进竖着出
判断屏幕方向主要使用这个方法

[UIApplication sharedApplication].statusBarOrientation

但是在自己的app测试发现,该值永远是UIInterfaceOrientationPortrait,app明明是横屏显示,statusBarOrientation出来的却是竖屏的值。这个statusBarOrientation值不生效,只能换另一种思路。当前思路是如果rootViewController支持竖屏,那么界面就是竖着进竖着出;如果rootViewController不支持竖屏(比如我们的app),那么界面就是横着进横着出。

 UIViewController* vc = [[[UIApplication sharedApplication].delegate window] rootViewController];
    // 支持竖屏
    if(vc.supportedInterfaceOrientations & UIInterfaceOrientationMaskPortrait ){
        return UIInterfaceOrientationMaskPortrait ;
    }else{
        //不支持竖屏
        return UIInterfaceOrientationMaskLandscapeRight;
    }

参考文章

1、https://www.jianshu.com/p/e473749f1c30
2、https://www.jianshu.com/p/bdde055b1d29
3、http://sxcheen.com/2016/04/29/iOS-Orientations/
4、https://www.jianshu.com/p/d6cb54d2eaa1
5、https://blog.csdn.net/wz_yinglong/article/details/72781795

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值