我们知道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文档中缺少另一种方法吗?
答案 0 :(得分: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
}
}
}
答案 1 :(得分: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
希望有帮助。...
谢谢
答案 2 :(得分:4)
这就是我正在做的:
enum WatchModel {
case w38, w40, w42, w44, unknown
}
extension WKInterfaceDevice {
static var currentWatchModel: WatchModel {
switch WKInterfaceDevice.current().screenBounds.size {
case CGSize(width: 136, height: 170):
return .w38
case CGSize(width: 162, height: 197):
return .w40
case CGSize(width: 156, height: 195):
return .w42
case CGSize(width: 184, height: 224):
return .w44
default:
return .unknown
}
}
}
答案 3 :(得分:3)
你的方法看起来很好,没有任何问题。另一种解决方案是使用WKInterfaceController的contentFrame属性。如果宽度为312(156)像素,那么其42毫米也是38毫米。
答案 4 :(得分: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
}
答案 5 :(得分:2)
检查screenBounds似乎不再适用于带有真实设备的iOS 9的xCode 7,Watch 38mm返回的手表大小始终为156x195。
我的(不好)替代方案是根据视图结构检查viewcontroller contentFrame的宽度或高度
答案 6 :(得分:1)
以上所有解决方案都运行良好。与screenBounds([[WKInterfaceDevice currentDevice] screenBounds])一起,也可以检查screenScale([[WKInterfaceDevice currentDevice] screenScale])。从这个意义上说,实际大小将是screenBounds * screenScale。
更多参考:https://developer.apple.com/watch/human-interface-guidelines/specifications/
答案 7 :(得分: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
}
}
答案 8 :(得分:1)
我这样做是为了避免自己过于依赖当前的大小和将来的证明
public enum WatchSizeClass {
case small, regular
}
public extension WKInterfaceDevice {
static var currentWatchSizeClass: WatchSizeClass {
if WKInterfaceDevice.current().screenBounds.size.width < 150 {
return .small
} else {
return .regular
}
}
}
现在,.small仅返回38mm。
答案 9 :(得分:0)
Swift 5 版本的@Aaron Brager答案(+支持新的Apple 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
}
}
}
/*
all resolutions
40mm: 394×324
44mm: 448×368
38mm: 340×272
42mm: 390×312
*/