如何判断当前运行的Apple Watch尺寸/尺寸是38mm还是42mm?

问题描述 投票:13回答:7

我们知道Apple Watch有两种屏幕尺寸:38mm和42mm。 WKInterfaceDevice类提供了一个名为screenBounds的可读属性。我为WKInterfaceDevice编写了一个扩展,尝试添加一种方法来检测当前的设备类型。

import WatchKit

enum WatchResolution {

    case Watch38mm, Watch42mm
}

extension WKInterfaceDevice {

    class func currentResolution() -> WatchResolution {

        let watch38mmRect = CGRectMake(0.0, 0.0, 136.0, 170.0)
        let watch42mmRect = CGRectMake(0.0, 0.0, 156.0, 195.0)

        let currentBounds = WKInterfaceDevice.currentDevice().screenBounds

        if CGRectEqualToRect(currentBounds, watch38mmRect) {

            return WatchResolution.Watch38mm
        } else {

            return WatchResolution.Watch42mm
        }
    }
}

这是检测Apple Watch尺寸的正确方法吗?我在Apple文档中缺少另一种方法吗?

swift watchkit apple-watch dimension
7个回答
4
投票

更新Swift 4:

它包括新推出的Watch分辨率:

enum WatchResolution {
    case Watch38mm, Watch40mm,Watch42mm,Watch44mm, Unknown  
}

extension WKInterfaceDevice {
class func currentResolution() -> WatchResolution {
    let watch38mmRect = CGRect(x: 0, y: 0, width: 136, height: 170)
    let watch40mmRect = CGRect(x: 0, y: 0, width: 162, height: 197)
    let watch42mmRect = CGRect(x: 0, y: 0, width: 156, height: 195)
    let watch44mmRect = CGRect(x: 0, y: 0, width: 184, height: 224)

    let currentBounds = WKInterfaceDevice.current().screenBounds

    switch currentBounds {
    case watch38mmRect:
        return .Watch38mm
    case watch40mmRect:
        return .Watch40mm
    case watch42mmRect:
        return .Watch42mm
    case watch44mmRect:
        return .Watch44mm
    default:
        return .Unknown
    }
  } 
}

用法

let resol = WKInterfaceDevice.currentResolution()
    switch resol {
    case .Watch38mm, .Watch42mm:
        // Do Something
    case .Watch40mm, .Watch44mm:
        // Do Something
    default:
        // Do Something
    }

参考链接:Apple Developer Watch Interface Link

希望有所帮助....

谢谢


23
投票

您的代码看起来不错,但有一些小问题:

  • 您没有“未知”屏幕大小的案例(可能在将来发布)
  • 你正在使用CGRectMake,但在Swift中你应该使用CGRect初始化器
  • 你正在使用CGRectEqualToRect,但在Swift你可以使用==switch
  • 你明确地返回WatchResolution枚举,但你不需要明确--Swift会从你的方法签名中找出它
  • 你宣布watch42mmRect但没有用它做任何事情

我会像这样重写它:

enum WatchResolution {
    case Watch38mm, Watch42mm, Unknown
}

extension WKInterfaceDevice {
    class func currentResolution() -> WatchResolution {
        let watch38mmRect = CGRect(x: 0, y: 0, width: 136, height: 170)
        let watch42mmRect = CGRect(x: 0, y: 0, width: 156, height: 195)

        let currentBounds = WKInterfaceDevice.currentDevice().screenBounds

        switch currentBounds {
        case watch38mmRect:
            return .Watch38mm
        case watch42mmRect:
            return .Watch42mm
        default:
            return .Unknown
        }
    }
}

3
投票

你的方法看起来很好,没有任何问题。另一种解决方案是使用WKInterfaceController的contentFrame属性。如果宽度为312(156)像素,那么其42毫米也是38毫米。


3
投票
CGRect rect = [WKInterfaceDevice currentDevice].screenBounds;
if (rect.size.height == 195.0) {
    // Apple Watch 42mm
}else if (rect.size.height == 170.0){
    // Apple Watch 38mm 
}

2
投票

检查screenBounds似乎不再适用于带有真实设备的iOS 9的xCode 7,Watch 38mm返回的手表大小始终为156x195。

我(坏)的替代方案是根据视图结构检查viewcontroller contentFrame的宽度或高度


1
投票

以上所有解决方案都运行良好。与screenBounds([[WKInterfaceDevice currentDevice] screenBounds])一起,也可以检查screenScale([[WKInterfaceDevice currentDevice] screenScale])。从这个意义上说,实际大小将是screenBounds * screenScale。

更多参考:https://developer.apple.com/watch/human-interface-guidelines/specifications/


1
投票

对于Swift 4较短的例子:

enum WatchType {
    case watch38mm, watch42mm
}

extension WKInterfaceDevice {

    class var currentResolution: WatchType {
        // Apple Watch 38mm 136x170 - 42mm 156x195
        return WKInterfaceDevice.current().screenBounds.width == 136 ? .watch38mm : .watch42mm
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.