我想在我的应用程序中使用一些触觉。我正在使用,UISelectionFeedbackGenerator但它不起作用。我正在使用 iOS 10 在真正的 iPhone 7 上进行测试。这两行应该可以发挥作用——但什么也没有发生:
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
我想在我的应用程序中使用一些触觉。我正在使用,UISelectionFeedbackGenerator但它不起作用。我正在使用 iOS 10 在真正的 iPhone 7 上进行测试。这两行应该可以发挥作用——但什么也没有发生:
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
似乎AVFoundation(或可能只是AVCaptureSession)以某种方式扰乱了触觉反馈!没有AVFoundation,我的触觉反馈有效。一旦我把它放在那里,它就停止工作了。
以下是一些适用于旧设备的替代方案:
import AudioToolbox
AudioServicesPlaySystemSound(1519) // Actuate `Peek` feedback (weak boom)
AudioServicesPlaySystemSound(1520) // Actuate `Pop` feedback (strong boom)
AudioServicesPlaySystemSound(1521) // Actuate `Nope` feedback (series of three weak booms)
在你打电话之前selectionChanged(),你需要打电话generator.prepare()。这会唤醒 Taptic 引擎。不过它会在几秒钟内保持活动状态,所以prepare()如果你要selectionChanged()在路上打电话,请确保再次激活它。
let generator = UISelectionFeedbackGenerator()
generator.prepare()
generator.selectionChanged()
对于那些正在苦苦挣扎的人AVFoundation,有一种方法可以从iOS13
func setAllowHapticsAndSystemSoundsDuringRecording(_ inValue: Bool) throws
您可以在从音频输入录制时启用系统声音和触觉播放。
对于使用AVFoundation和不使用触觉的人,我找到了一种使触觉AVFoundation同时工作的简单方法:</p>
func yourAVRecording() {
// Haptic
let generator = UISelectionFeedbackGenerator()
generator.prepare()
generator.selectionChanged()
// Record after played Haptic (just give few time to let haptic done and then magic happen)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
// Do your AVFoundation stuffs whatever here
}
}