构建独立 watchOS 应用与连接蓝牙 LE 设备
1. 让手表振动
要让 Apple Watch 振动,可以使用苹果提供的单例 WKInterfaceDevice.captureDevice()
的 playHaptic(_:)
方法。苹果通过 WKHapticType
枚举提供了几种预配置的振动类型,其中 Notification
类型最为强烈,适合用于像 CarFinder 应用这样提醒用户返回车辆的场景。
以下是实现手表振动的代码示例:
func showAlert(timer: NSTimer) {
var reminderMessage = "Please return to your car"
if let userInfo = timer.userInfo as? [String: String] {
reminderMessage+="at \(userInfo["address"])"
}
print("Meter is out of time.")
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Notification)
let okAction = WKAlertAction(title: "OK", style: WKAlertActionStyle.Default) { () -> Void in
print("OK b