xcode 如何在 Apple Watch OS 2 上触发振动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32362447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:45:39  来源:igfitidea点击:

How to trigger vibration on Apple Watch OS 2?

xcodeswift2apple-watchaudiotoolboxwatchos-2

提问by Jiri

I have been working with Apple Watch OS 2 for a while, but have not succeeded in triggering vibration on Apple Watch standalone app in Swift, using Xcode Version 7.0 beta 6 (7A192o).

我一直在使用 Apple Watch OS 2,但没有成功在 Swift 中的 Apple Watch 独立应用程序上触发振动,使用 Xcode 版本 7.0 beta 6 (7A192o)。

All tutorials using vibration on iPhone in Swift look like:

在 Swift 中使用 iPhone 振动的所有教程如下所示:

 import AudioToolbox
 ...
 AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

But I cannot import the AudioToolboxlibrary.

但我无法导入AudioToolbox库。

I get the error:

我收到错误:

Could not build Objective-C module "AudioToolbox"

无法构建 Objective-C 模块“AudioToolbox”

Has anyone already found the way to do it?

有没有人已经找到了这样做的方法?

回答by LucPts

You can generate haptic & auditory feedback, using:

您可以使用以下方法生成触觉和听觉反馈:

WKInterfaceDevice.currentDevice().playHaptic(.Click)

'Click' is one of the 'cases' (types of vibration/beeps) that you can choose, cfr:

“点击”是您可以选择的“情况”之一(振动/蜂鸣声类型),参见:

enum WKHapticType : Int {? 

case Notification? 
case DirectionUp?
case DirectionDown? 
case Success?
case Failure? 
case Retry? 
case Start? 
case Stop? 
case Click?
}

This only works on the device, not in the simulator.

这仅适用于设备,不适用于模拟器。

回答by Zoltan

In Swift 3:

Swift 3 中:

WKInterfaceDevice.current().play(.success)

回答by Massimo Polimeni

I'm not a expert in WatchKitbut I know that with Watch OS 2you can use:

我不是WatchKit的专家,但我知道在Watch OS 2 中你可以使用:

WKInterfaceDevice().playHaptic(.Notification)

The function playHapticcalled on a WKInterfaceDeviceinstance engage the haptic engine in Apple Watch.

playHapticWKInterfaceDevice实例上调用的函数使用 Apple Watch 中的触觉引擎。

See more in the Apple documentation.

Apple 文档中查看更多信息

回答by Onvil

Function :

功能 :

WKInterfaceDevice().playHaptic(.Notification)

WKInterfaceDevice().playHaptic(.Notification)

Here you have : Documentation

你有: 文档

回答by Dylan Murphy

WKInterfaceDevice.current().play(.failure) 

in Swift 3

在斯威夫特 3

回答by Manish Gumbal

The Correct format is

正确的格式是

WKInterfaceDevice.currentDevice().playHaptic(.Notification)

You can easily play the notification sound on watch

您可以轻松地在手表上播放通知声音