获取设备当前方向(App Extension)

Ana*_*har 16 keyboard objective-c ios uideviceorientation ios-app-extension

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

  1. 它总是返回UIDeviceOrientationUnknown

    [[UIDevice currentDevice] orientation]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 它显示红色消息,'sharedApplication'在iOS上不可用(App Extension)

    [[UIApplication sharedApplication] statusBarOrientation];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我还添加了一个观察者,但它没有被调用.

    [[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"];
    }
    
    Run Code Online (Sandbox Code Playgroud)

那么现在如何才能获得当前的设备定位.

mmt*_*tmm 13

我有个主意!

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
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

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

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ana*_*har 9

我找到了一种方法,我们可以这样计算我们的设备方向(App Extension)

- (void)viewDidLayoutSubviews
{
   if(self.view.frame.size.width > self.view.frame.size.height)
      NSLog(@"Landscape");
   else
      NSLog(@"Portrait");
}
Run Code Online (Sandbox Code Playgroud)

它给了我正确的方向,但仍然没有获得设备是LandscapeLeftLandscapeRight以及PortraitPortraitUpsideDown.

仍然需要帮助.

  • 您应该使用屏幕边界而不是视图框架,因为它并不总是正确的,在我的情况下,视图宽度始终大于高度,但使用屏幕边界对我来说是CGRect r = [UIScreen mainScreen] .bounds; BOOL isLandscape = r.size.width> r.size.height; (3认同)

Aur*_*ura 6

如果您之前添加以下内容,则会调用observer方法: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

编辑:我UIApplicationDidChangeStatusBarOrientationNotification用于观察者

在我的方法中,我检查:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; BOOL isPortrait = UIDeviceOrientationIsPortrait(orientation);

编辑2- Xcode 6.2 - iOS 7和8

似乎如果你想在iOS 7和8上使用它,上面的代码将在iOS 7上给你错误的结果.

所以我使用其他东西,因为在iOS 7上,mainScreen的界限永远不会改变,但是如果方向改变,iOS 8将会改变.

无论iOS版本如何,我都有3个宏可以为我提供正确尺寸的屏幕宽度和高度:

#define IOS_VERSION_OLDER_THAN_8 ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)

#define SCREEN_WIDTH_CALCULATED (IOS_VERSION_OLDER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height) : [[UIScreen mainScreen] bounds].size.width)

#define SCREEN_HEIGHT_CALCULATED (IOS_VERSION_OLDER_THAN_8 ? (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width) : [[UIScreen mainScreen] bounds].size.height)
Run Code Online (Sandbox Code Playgroud)

然后,当通知被触发时,我检查方向如下:

BOOL isPortrait = SCREEN_WIDTH_CALCULATED < SCREEN_HEIGHT_CALCULATED;
Run Code Online (Sandbox Code Playgroud)

这适用于iOS 7和iOS 8,但我没有检查旧版本的Xcode,只有6.2

仅当设备处于纵向或横向时才会返回,而不是所有4种方向类型


Bws*_*luk 6

在 BroadcastExtension 中,您可以使用 sampleBuffer 来了解方向:

if let orientationAttachment =  CMGetAttachment(sampleBuffer, RPVideoSampleOrientationKey as CFString, nil) as? NSNumber 
{  
          let orientation = CGImagePropertyOrientation(rawValue: orientationAttachment.uint32Value)   
}
Run Code Online (Sandbox Code Playgroud)


Riv*_*era 0

你可以看看这个库及其分支。它使用 CoreMotion 来检测设备方向,因此它可以在应用程序扩展的受限环境中工作。