我正在 SwiftUI 中编写一个计时器。 当我的计时器完成后,我想要进行触觉返回,我想要像手机上的闹钟一样相当重的振动。
问题是,当触觉返回发生时,没有任何反应,并且我在终端上收到错误消息。
错误如下:
CAReportingClient.mm:532 Attempted to remove a reporter not created by this client { careporter_id=16 578 573 762 561 }
这是我的代码:
import Foundation
import SwiftUI
import CoreHaptics
struct Exercice_TimerButton: View {
@State var totalSeconds: Int
@State public var remainingSeconds: Int
@State private var timer: Timer?
var body: some View {
ZStack(alignment:.leading) {
Rectangle()
.foregroundColor(Constants.color_background2)
.frame(width: width, height: height)
.cornerRadius(20)
Rectangle()
.foregroundColor(Constants.color_primaire2)
.frame(width: calculateProgressBarWidth(), height: height)
.cornerRadius(20)
Text("\(remainingSeconds) secondes")
.font(.system(size: 15, weight: .regular, width: .expanded))
.foregroundColor(Constants.color_text2)
.frame(width: UIScreen.main.bounds.width - 60, height:height)
}
.frame(width:CGFloat(UIScreen.screenWidth - 60),height:40)
.onAppear {
startTimer()
}
.padding(.all,35)
}
private func startTimer() {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
withAnimation(.linear(duration: 1.0)) {
updateTimer()
}
}
}
private func updateTimer() {
guard remainingSeconds > 0 else {
// Timer completed, stop the timer
timer?.invalidate()
generateAlarmVibration() //HERE I WANT TO EXECUTE THE VIBRATIONS
return
}
remainingSeconds -= 1
}
private func calculateProgressBarWidth() -> CGFloat {
let progressPercentage = CGFloat(remainingSeconds) / CGFloat(totalSeconds)
return width * (1.0 - progressPercentage)
}
//FROM HERE ON, THE PART FOR HAPTIC FEEDBACK
private func generateAlarmVibration() {
// Create haptic events
let hapticEvents = createHapticEvents()
// Play haptic events
playHapticEvents(events: hapticEvents)
}
private func createHapticEvents() -> [CHHapticEvent] {
var events: [CHHapticEvent] = []
// First loop
for i in stride(from: 0, to: 1, by: 0.1) {
let intensity = CHHapticEventParameter(parameterID: .hapticIntensity, value: Float(i))
let sharpness = CHHapticEventParameter(parameterID: .hapticSharpness, value: Float(i))
let event = CHHapticEvent(eventType: .hapticTransient, parameters: [intensity, sharpness], relativeTime: i)
events.append(event)
}
// Second loop
for i in stride(from: 0, to: 1, by: 0.1) {
let intensity = CHHapticEventParameter(parameterID: .hapticIntensity, value: Float(1 - i))
let sharpness = CHHapticEventParameter(parameterID: .hapticSharpness, value: Float(1 - i))
let event = CHHapticEvent(eventType: .hapticTransient, parameters: [intensity, sharpness], relativeTime: 1 + i)
events.append(event)
}
return events
}
private func playHapticEvents(events: [CHHapticEvent]) {
// Check if the device supports haptic feedback
guard CHHapticEngine.capabilitiesForHardware().supportsHaptics else {
print("Device does not support haptic feedback.")
return
}
do {
// Create the haptic engine
let engine = try CHHapticEngine()
try engine.start()
// Create a haptic pattern
let pattern = try CHHapticPattern(events: events, parameters: [])
// Create a haptic player and play the pattern
let player = try engine.makePlayer(with: pattern)
try player.start(atTime: 0)
} catch let error {
print("Error creating haptic engine or playing pattern: \(error.localizedDescription)")
}
}
}
如果您有任何想法,我很乐意听取您的意见, 预先感谢。
我已经尝试了几种振动方法,经典的振动(成功、错误......)效果很好,但我想要更高级的东西。每次铃声响起,我都会收到相同的错误消息。
嘿,我确实有完全相同的代码和相同的问题,我喜欢你实现更高级触觉的方式,并且除了成功完全错误之外我有相同的想法......并且想要更多的控制。我初始化了核心触觉框架的 CHEngine 类似的东西并经常使用它,但随后我得到的 CAREportingClient.mm:532 与你每次使用不同的carereporter ID号完全相同
如果您找到修复程序,请告诉我(+如果我成功,我会通知您)这是新的,从 iOS 17 才开始,对吗?以前从未见过它,现在它堵塞了 XCode 日志并控制台每次与触觉的交互。