xcode 手表套件:是否可以以编程方式振动手表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28807465/
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 06:45:04  来源:igfitidea点击:

Watch Kit: is it possible to vibrate watch programmatically?

objective-cxcodewatchkit

提问by kelin

Is it possible to vibrate watch while Watch Extension is running? We can do it on iOS in this way (force iPhone to vibrate):

Watch Extension 运行时是否可以振动手表?我们可以在 iOS 上这样操作(强制 iPhone 振动):

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

I hope there is something similar on WatchKit.

我希望 WatchKit 上有类似的东西。

Update:I have added issue to Apple radar and recieved the answer:

更新:我已经向 Apple 雷达添加了问题并收到了答案:

Engineering has determined that your bug report (20019274) is a duplicate of another issue (19025053) and will be closed.

工程已确定您的错误报告 (20019274) 是另一个问题 (19025053) 的副本,将被关闭。

19025053 is still open.

19025053 仍然开放。

Update 2:AudioServicesPlayAlertSound()not working on watch simulator with any sound ID. Seems like function is not supported.

更新 2:AudioServicesPlayAlertSound()不适用于具有任何声音 ID 的手表模拟器。似乎不支持功能。

回答by Ste Prescott

You can now ask the Watch to vibrate if you target watchOS 2.0

如果您的目标是watchOS 2.0,您现在可以让手表振动

To do this all you need to do is call playHapticon a WKInterfaceDeviceinstance with any WKHapticType. In the example below it will play the notification haptic.

要做到这一点,您需要做的就是使用 any调用playHaptic一个WKInterfaceDevice实例WKHapticType。在下面的示例中,它将播放通知触觉。

Swift 3

斯威夫特 3

WKInterfaceDevice.current().play(.notification)

Objective-C

目标-C

[[WKInterfaceDevice currentDevice] playHaptic:WKHapticTypeNotification];

You can further read the Apple WKInterfaceDevice Documentation

您可以进一步阅读 Apple WKInterfaceDevice 文档

回答by cnoon

That's a great question, but unfortunately the answer is no. WatchKit doesn't have any APIs available to control haptic feedback. If you would really like to see this feature supported, I'd suggest you file a radaras a feature request.

这是一个很好的问题,但不幸的是答案是否定的。WatchKit 没有任何可用于控制触觉反馈的 API。如果您真的希望看到支持此功能,我建议您将雷达作为功​​能请求提交。

回答by 4GetFullOf

This is the answer in objective-c after watchOS 2

这是watchOS 2之后objective-c中的答案

[[WKInterfaceDevice currentDevice] playHaptic:WKHapticTypeNotification];

回答by John

With WatchKit, you have to remember that your code runs on the iPhone and not on the watch. Therefore, AudioServicesPlaySystemSound call from a WatchKit extension would run on the iPhone, not on the watch. It will make the iPhone vibrate.

使用 WatchKit,您必须记住您的代码在 iPhone 上运行,而不是在手表上运行。因此,来自 WatchKit 扩展的 AudioServicesPlaySystemSound 调用将在 iPhone 上运行,而不是在手表上运行。它会使 iPhone 振动。