With iOS 17, we no longer need the UIFeedbackGenerator class we previously used to interact with users. We can enable users to interact with SwiftUI components and provide feedback using .sensoryFeedback.
This project enables easy integration of haptic feedback in iOS applications. Haptic feedback is an important feature that enriches the user experience and makes interactions more responsive. This library supports various types of haptic feedback and simplifies the implementation of haptic feedback for developers.
This is a modifier used to provide users with tactile feedback. This feedback can help users understand when an action is completed, a selection is made, or an action succeeds or fails. Native component support is provided by eliminating the need for UIKit (works on iOS 17 and above 😞).
@State private var isOn = false
@State var stepperValue: Int = 50
@State private var completeWarning = false
@State private var impactLight = false
Button("Light") {
impactLight.toggle()
}
.buttonStyle(.borderedProminent)
.sensoryFeedback(.impact(weight: .light), trigger: impactLight)
Button("Error") {
completeError.toggle()
}
.buttonStyle(.borderedProminent)
.tint(.red)
.sensoryFeedback(.start, trigger: completeWarning)
Toggle("Toogle", isOn: $isOn)
.sensoryFeedback(.selection, trigger: isOn)
Stepper("Stepper \(stepperValue)", value: $stepperValue, in: 1...100, step: 1)
.sensoryFeedback(trigger: stepperValue){oldValue, newValue in
return oldValue < newValue ? .increase : .decrease
}
It is a class used to generate feedback on the iOS platform from UIKit components. It has three different subclasses, each representing a specific type of feedback.
This class provides the user with feedback as if a physical impact has occurred.
UIImpactFeedbackGenerator(style: .light).impactOccurred()
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
This class provides feedback to the user indicating that there is a notification or alert.
UINotificationFeedbackGenerator().notificationOccurred(.success)
UINotificationFeedbackGenerator().notificationOccurred(.warning)
UINotificationFeedbackGenerator().notificationOccurred(.error)
This class provides feedback when a selection is made in the user interface.
Toggle("Toogle", isOn: $isOn)
.onChange(of: isOn) {
UISelectionFeedbackGenerator().selectionChanged()
}