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   
}

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

希望会帮助你.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...