History
Loading...
Loading...
September 3, 2025
UIImpactFeedbackGenerator() in your SwiftUI views to provide immediate tactile feedback. Call impactOccurred() on user interactions like button taps or swipe gestures to make your app feel more responsive and engaging.This centralized haptic manager provides a consistent interface for all haptic feedback throughout your app, with prepared generators for better performance and standardized feedback types.
class HapticManager {
static let shared = HapticManager()
private let impact = UIImpactFeedbackGenerator(style: .medium)
private let notification = UINotificationFeedbackGenerator()
private init() {
impact.prepare()
}
func impact(_ style: UIImpactFeedbackGenerator.FeedbackStyle = .medium) {
UIImpactFeedbackGenerator(style: style).impactOccurred()
}
func notification(_ type: UINotificationFeedbackGenerator.FeedbackType) {
notification.notificationOccurred(type)
}
}
// Usage in SwiftUI
struct ContentView: View {
var body: some View {
Button("Tap me") {
HapticManager.shared.impact(.light)
// Your button action
}
}
}Centralizing haptic feedback prevents scattered haptic code across views, ensures consistent user experience, improves performance through generator preparation, and makes it easy to disable haptics globally for accessibility or user preferences.