ios – 在应用程式额外资讯中检测方向的最佳方式是什么?

在应用程序扩展中检测设备方向的最佳方法是什么?我在这里找到的解决方案有不同的结果:

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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...