HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在应用程式额外资讯中检测方向的最佳方式是什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@在应用程序扩展中检测设备方向的最佳方法是什么?我在这里找到的解决方案有不同的结果:

How to detect Orientation Change in Custom Keyboard Extension in iOS 8?

Get device current orientation (App Extension)

我看过大小类和UITraitCollection,发现设备不准确地报告说它实际上是横向的(不知道这是否是操作系统的错误,或者我没有正确的方式来查询正确的API).

什么是最好的方法来完成:

>当第一次加载扩展时,设备的当前方向
>设备将旋转到的方向
>设备旋转到的方向

谢谢,

解决方法

面对这个问题,也透过你的例子,但并没有很好的解决办法.
我如何解决它:我创建了一个类,可以对UIScreen值进行一些计算,并返自定义的设备方向.

标题

typedef NS_ENUM(NSInteger,InterfaceOrientationTypE) {
    InterfaceOrientationTypePorTrait,InterfaceOrientationTypeLandscape
};

@interfacE interfaceOrientation : NSObject

+ (InterfaceOrientationTypE)orientation;

@end

执行:

@implementation InterfaceOrientation

+ (InterfaceOrientationTypE)orientation{

    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize nativeSize = [UIScreen mainScreen].currentMode.size;
    CGSize sizeInPoints = [UIScreen mainScreen].bounds.size;

    InterfaceOrientationType result;

    if(scale * sizeInPoints.width == nativeSize.width){
        result = InterfaceOrientationTypePorTrait;
    }else{
        result = InterfaceOrientationTypeLandscape;
    }

    return result;
}

@end

我把它放到viewWillLayoutSubviews或viewDidLayoutSubviews方法来捕获方向改变事件.

if([InterfaceOrientation orientation] == InterfaceOrientationTypePorTrait){
     // porTrait   
}else{
     // landscape   
}

如果您想要确定设备方向(左,右,上下颠倒),则此方法无法解决您的问题.它只是返回肖像或风景方向.

希望会帮助你.

大佬总结

以上是大佬教程为你收集整理的ios – 在应用程式额外资讯中检测方向的最佳方式是什么?全部内容,希望文章能够帮你解决ios – 在应用程式额外资讯中检测方向的最佳方式是什么?所遇到的程序开发问题。

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

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