我想在我的应用程序中使用一些触觉反馈。我正在使用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)
若你正为 AVFoundation
而烦恼,从 iOS13
开始有一种方法可供使用。
func setAllowHapticsAndSystemSoundsDuringRecording(_ inValue: Bool) throws
selectionChanged()
之前,您需要调用 generator.prepare()
。这会唤醒触觉引擎。它会保持活动状态几秒钟,因此如果您将来要调用 selectionChanged()
,请确保再次调用 prepare()
。let generator = UISelectionFeedbackGenerator()
generator.prepare()
generator.selectionChanged()
if #available(iOS 10.0, *) { ... }
中。但是即使是在iOS 10上,没有条件,它也无法在我的iPhone 7上工作。这也正是我在发布到stackoverflow之前尝试的。 - Kevin LieserAVCaptureSession
时,是否存在触觉反馈的限制? - Kevin Lieser对于使用 AVFoundation
但无法与触觉一起工作的人,我发现了一个简单的方法,可以使触觉与 AVFoundation
同时工作:
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
}
}