HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在iOS中模拟方向更改以进行测试大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想测试我的应用程序处理方向更改(纵向/横向)的能力.我目前正在使用 KIF,据我所知,它无法做到这一点.有没有办法以编程方式为iOS模拟器模拟旋转事件?

我不在乎它是否是一些未记录的私有API或黑客,因为这只会在测试期间运行,并且不会成为生产构建的一部分.

解决方法

这是实现这一目标的一步:
+ (KIFTestSteP*) stepToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation {

    NSString* orientation = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? @"Landscape" : @"PorTrait";
        return [KIFTestStep stepWithDescription: [NSString StringWithFormat: @"Rotate to orientation %@",orientation]
                             executionBlock: ^KIFTestStepResult(KIFTestStep *step,NSError *__autoreleasing *error) {
                                 if( [UIApplication sharedApplication].statusBarOrientation != toInterfaceOrientation ) {
                                     UIDevice* device = [UIDevice currentDevice];
                                     SEL message = NSSELEctorFromString(@"setOrientation:");

                                     if( [device respondsToSELEctor: message] ) {
                                         NSMethodSignature* signature = [UIDevice instanceMethodSignatureForSELEctor: message];
                                         NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
                                         [invocation setTarget: device];
                                         [invocation setSELEctor: message];
                                         [invocation setArgument: &toInterfaceOrientation aTindex: 2];
                                         [invocation invoke];
                                     }
                                 }

                                 return KIFTestStepResultsuccess;
                             }];
}

注意:将设备平放在桌面上,或者加速度计更新会将视图旋转回来.

大佬总结

以上是大佬教程为你收集整理的在iOS中模拟方向更改以进行测试全部内容,希望文章能够帮你解决在iOS中模拟方向更改以进行测试所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。