我们知道Apple Watch有两种屏幕尺寸:38毫米和42毫米。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大小的正确方法吗?我在苹果文档中还遗漏了其他方法吗?
发布于 2020-02-08 04:46:16
这就是我正在做的事情:
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
}
}
}
发布于 2015-04-09 13:13:40
您的方法看起来很好,没有任何问题。另一种解决方案是使用WKInterfaceController的contentFrame属性。如果宽度为312(156)像素,则其42 is为38 is。
发布于 2015-04-09 13:38:04
CGRect rect = [WKInterfaceDevice currentDevice].screenBounds;
if (rect.size.height == 195.0) {
// Apple Watch 42mm
}else if (rect.size.height == 170.0){
// Apple Watch 38mm
}
https://stackoverflow.com/questions/29528792
复制相似问题