如何判断当前 运行 Apple Watch size/dimension 是 38mm 还是 42mm?
How to tell if current running Apple Watch size/dimension is 38mm or 42mm?
我们知道 Apple Watch 有两种屏幕尺寸:38mm 和 42mm。 WKInterfaceDevice
class 提供了一个名为 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 文档中是否缺少其他方法?
你的方法看起来不错,没有任何问题。另一种解决方案是使用 WKInterfaceController 的 contentFrame 属性。如果宽度为 312(156)像素,则其 42mm 为 38mm。
您的代码看起来不错,但有一些小问题:
- 您没有 "unknown" 屏幕尺寸的案例(将来可能会发布)
- 您正在使用
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
}
}
}
CGRect rect = [WKInterfaceDevice currentDevice].screenBounds;
if (rect.size.height == 195.0) {
// Apple Watch 42mm
}else if (rect.size.height == 170.0){
// Apple Watch 38mm
}
以上所有解决方案都可以正常工作。除了 screenBounds ([[WKInterfaceDevice currentDevice] screenBounds]),最好也检查 screenScale ([[WKInterfaceDevice currentDevice] screenScale])。从这个意义上说,实际大小将是 screenBounds * screenScale。
更多参考:https://developer.apple.com/watch/human-interface-guidelines/specifications/
在 xCode 7 和 iOS 9 上检查 screenBounds 似乎不再适用于真实设备,Watch 38mm 返回的手表尺寸始终为 156x195。
我的(错误的)替代方法是根据视图结构检查 viewcontroller contentFrame 的宽度或高度
对于 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
}
}
更新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
}
参考资料Link:
Apple Developer Watch Interface Link
希望对您有所帮助....
谢谢
Swift @Aaron Brager 回答的 5 版本(+ 支持新的 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
*/
这就是我正在做的事情:
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
}
}
}
我这样做是为了避免自己过分依赖当前的尺寸和未来的证明
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 返回。
根据此处的其他人修改答案。认为看起来更合适,加上包括新的 Watch Series 7
:
#if os(watchOS)
import WatchKit
public enum WatchCase {
case watch38mm
case watch40mm
case watch41mm
case watch42mm
case watch44mm
case watch45mm
case unknown
}
extension WKInterfaceDevice {
public var screenCase: WatchCase {
switch WKInterfaceDevice.current().screenBounds.size {
case CGSize(width: 136, height: 170):
return .watch38mm
case CGSize(width: 162, height: 197):
return .watch40mm
case CGSize(width: 176, height: 215):
return .watch41mm
case CGSize(width: 156, height: 195):
return .watch42mm
case CGSize(width: 184, height: 224):
return .watch44mm
case CGSize(width: 198, height: 242):
return .watch45mm
default:
return .unknown
}
}
}
#endif
用法:
WKInterfaceDevice.current().screenCase
我们知道 Apple Watch 有两种屏幕尺寸:38mm 和 42mm。 WKInterfaceDevice
class 提供了一个名为 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 文档中是否缺少其他方法?
你的方法看起来不错,没有任何问题。另一种解决方案是使用 WKInterfaceController 的 contentFrame 属性。如果宽度为 312(156)像素,则其 42mm 为 38mm。
您的代码看起来不错,但有一些小问题:
- 您没有 "unknown" 屏幕尺寸的案例(将来可能会发布)
- 您正在使用
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
}
}
}
CGRect rect = [WKInterfaceDevice currentDevice].screenBounds;
if (rect.size.height == 195.0) {
// Apple Watch 42mm
}else if (rect.size.height == 170.0){
// Apple Watch 38mm
}
以上所有解决方案都可以正常工作。除了 screenBounds ([[WKInterfaceDevice currentDevice] screenBounds]),最好也检查 screenScale ([[WKInterfaceDevice currentDevice] screenScale])。从这个意义上说,实际大小将是 screenBounds * screenScale。
更多参考:https://developer.apple.com/watch/human-interface-guidelines/specifications/
在 xCode 7 和 iOS 9 上检查 screenBounds 似乎不再适用于真实设备,Watch 38mm 返回的手表尺寸始终为 156x195。
我的(错误的)替代方法是根据视图结构检查 viewcontroller contentFrame 的宽度或高度
对于 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
}
}
更新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
}
参考资料Link: Apple Developer Watch Interface Link
希望对您有所帮助....
谢谢
Swift @Aaron Brager 回答的 5 版本(+ 支持新的 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
*/
这就是我正在做的事情:
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
}
}
}
我这样做是为了避免自己过分依赖当前的尺寸和未来的证明
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 返回。
根据此处的其他人修改答案。认为看起来更合适,加上包括新的 Watch Series 7
:
#if os(watchOS)
import WatchKit
public enum WatchCase {
case watch38mm
case watch40mm
case watch41mm
case watch42mm
case watch44mm
case watch45mm
case unknown
}
extension WKInterfaceDevice {
public var screenCase: WatchCase {
switch WKInterfaceDevice.current().screenBounds.size {
case CGSize(width: 136, height: 170):
return .watch38mm
case CGSize(width: 162, height: 197):
return .watch40mm
case CGSize(width: 176, height: 215):
return .watch41mm
case CGSize(width: 156, height: 195):
return .watch42mm
case CGSize(width: 184, height: 224):
return .watch44mm
case CGSize(width: 198, height: 242):
return .watch45mm
default:
return .unknown
}
}
}
#endif
用法:
WKInterfaceDevice.current().screenCase