获取设备当前方向(App Extension) [英] Get device current orientation (App Extension)

查看:549
本文介绍了获取设备当前方向(App Extension)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在App Extension中获取设备当前方向,我尝试了以下两种方法但没有成功。


  1. 它总是返回UIDeviceOrientationUnknown

      [[UIDevice currentDevice] orientation] 


  2. 它显示红色消息,'sharedApplication'在iOS上不可用(App Extension)

      [[UIApplication sharedApplication] statusBarOrientation]; 


  3. 我还添加了一个观察者,但没有被调用。



    'pre> [[NSNotificationCenter defaultCenter]的addObserver:self.view选择器:@selector(notification_OrientationWillChange :)名:UIApplicationWillChangeStatusBarOrientationNotification对象:无];



    - (无效)notification_OrientationWillChange:(NSNotification *)N
    {
    UIInterfaceOrientation取向=(UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey ] intValue];

    if(orientation == UIInterfaceOrientationLandscapeLeft)
    [self.textDocumentProxy insertText:@Left];
    if(orientation == UIInterfaceOrientationLandscapeRight)
    [self.textDocumentProxy insertText:@Right];
    }


那么现在怎么样?任何人都可以获得当前的设备定位。

解决方案

我有个主意!

 延伸UIScreen {

VAR取向:UIInterfaceOrientation {
设点= coordinateSpace.convertPoint(CGPointZero,toCoordinateSpace:fixedCoordinateSpace)
如果点= = CGPointZero {
return .Portrait
}否则如果point.x!= 0&& point.y!= 0 {
return .PortraitUpsideDown
}否则如果point.x == 0&& point.y!= 0 {
return .LandscapeLeft
}否则如果point.x!= 0&& point.y == 0 {
return .LandscapeRight
} else {
return .Unknown
}
}

}

编辑:在Swift 4上你可以这样做:

 扩展UIScreen {
var orientation:UIInterfaceOrientation {
let point = coordinateSpace.convert(CGPoint.zero,to:fixedCoordinateSpace)
开关(point.x,point.y){
case(0,0):
return .portrait
case let(x,y)其中x!= 0&& y!= 0:
返回.portraitUpsideDown
case let(0,y)其中y!= 0:
返回.landscapeLeft
case let(x,0)其中x! = 0:
返回.landscapeRight
默认值:
返回.unknown
}
}
}


How to get device current orientation in an App Extension, I have tried below two methods but no success.

  1. It always return UIDeviceOrientationUnknown

    [[UIDevice currentDevice] orientation]
    

  2. It shows red message that ‘sharedApplication’ is not available on iOS (App Extension)

    [[UIApplication sharedApplication] statusBarOrientation];
    

  3. I also add an observer but it not getting called.

    [[NSNotificationCenter defaultCenter] addObserver:self.view selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    
    
    
    - (void)notification_OrientationWillChange:(NSNotification*)n
    {
       UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
    
        if (orientation == UIInterfaceOrientationLandscapeLeft)
           [self.textDocumentProxy insertText:@"Left"];
        if (orientation == UIInterfaceOrientationLandscapeRight)
           [self.textDocumentProxy insertText:@"Right"];
    }
    

So now how can anyone get current device orientation.

解决方案

I got an idea!

extension UIScreen {

    var orientation: UIInterfaceOrientation {
        let point = coordinateSpace.convertPoint(CGPointZero, toCoordinateSpace: fixedCoordinateSpace)
        if point == CGPointZero {
            return .Portrait
        } else if point.x != 0 && point.y != 0 {
            return .PortraitUpsideDown
        } else if point.x == 0 && point.y != 0 {
            return .LandscapeLeft
        } else if point.x != 0 && point.y == 0 {
            return .LandscapeRight
        } else {
            return .Unknown
        }
    }

}

EDIT: On Swift 4 you can do:

extension UIScreen {
    var orientation: UIInterfaceOrientation {
        let point = coordinateSpace.convert(CGPoint.zero, to: fixedCoordinateSpace)
        switch (point.x, point.y) {
        case (0, 0):
            return .portrait
        case let (x, y) where x != 0 && y != 0:
            return .portraitUpsideDown
        case let (0, y) where y != 0:
            return .landscapeLeft
        case let (x, 0) where x != 0:
            return .landscapeRight
        default:
            return .unknown
        }
    }
}

这篇关于获取设备当前方向(App Extension)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆