I’m creating an app that contains a compass tab. I would like for it to vibrate upon changing degrees of orientation. I got it to work, however, when I switch over to a new tab I can still feel the compass vibrating in the background.
import SwiftUI
import CoreLocation
struct CompassView : View {
@ObservedObject var compassHeading = CompassHeading()
var body: some View {
ZStack {
/* compass shapes and formatting */
}
.onChange(of: compassHeading.degrees) {
let generator = UIImpactFeedbackGenerator(style: .soft)
generator.impactOccurred()
}
}
}
2
Answers
.onChange(of: compassHeading.degrees)
is likely to generate a LOT of changes.You’re probably feeling all the haptics that you queued up still playing long after you view has gone.
Try debouncing your input via minimum change or limiting the number of haptic events that you can form. The first task will be to see how many events you’re creating with a
print
.CompassHeading Class:
CompassView: