How to tell if current running Apple Watch size/dimension is 38mm or 42mm?

好久不见. 提交于 2020-01-11 17:53:22

问题


We know that there are two screen sizes for Apple Watch: 38mm and 42mm. The WKInterfaceDevice class provides a readable property named screenBounds. I wrote an extension for WKInterfaceDevice, trying to add a method to detect current device type.

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

Is that the correct method to detect Apple Watch size? Is there another method I am missing in the Apple docs?


回答1:


Update Swift 4:

It includes new launch of Watch resolutions:

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

Usage

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

Reference Link: Apple Developer Watch Interface Link

Hope that helps....

Thanks




回答2:


Your code looks good, but has a few minor issues:

  • You don't have a case for an "unknown" screen size (possibly released in the future)
  • You're using CGRectMake but in Swift you should use a CGRect initializer
  • You're using CGRectEqualToRect but in Swift you can just use == or switch
  • You're explicitly returning WatchResolution enums, but you don't need to be explicit - Swift will figure it out from your method signature
  • You're declaring watch42mmRect but not using it for anything

I would rewrite it like this:

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:


Your method looks fine and nothing is wrong with it. Another solution is to use contentFrame property of the WKInterfaceController. If the width is 312(156) pixels then its 42mm else is 38mm.




回答4:


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



回答5:


Checking screenBounds seems not working anymore on xCode 7 with iOS 9 with a real device, the watch size returned by Watch 38mm is always 156x195.

My (bad) alternative is to check the viewcontroller contentFrame width or height depending of the view structure




回答6:


All the above solutions works fine. Along with screenBounds ([[WKInterfaceDevice currentDevice] screenBounds]), it will be good to check the screenScale ([[WKInterfaceDevice currentDevice] screenScale]) as well. Actual size will be screenBounds * screenScale in that sense.

More reference: https://developer.apple.com/watch/human-interface-guidelines/specifications/




回答7:


For a Swift 4 shorter example:

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
    }

}



回答8:


Swift 5 version of @Aaron Brager answer(+ support for new Apple Watches).

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


/*
 all resolutions
 40mm: 394×324
 44mm: 448×368
 38mm: 340×272
 42mm: 390×312
 */


来源:https://stackoverflow.com/questions/29528792/how-to-tell-if-current-running-apple-watch-size-dimension-is-38mm-or-42mm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!