How do I implement haptics and vibrations in SwiftUI/UIKit?

Implementing haptics and vibrations in SwiftUI or UIKit can greatly enhance the user experience by providing tactile feedback during interactions. Below is an example of how to utilize the built-in haptic feedback available in iOS.

Using the `UIImpactFeedbackGenerator`, you can generate different types of haptic feedback based on the intensity of the interaction.

// Import required modules import SwiftUI import UIKit struct ContentView: View { var body: some View { Button("Tap me") { // Trigger haptic feedback let generator = UIImpactFeedbackGenerator(style: .medium) generator.impactOccurred() } .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) } }

haptics vibrations SwiftUI UIKit UIImpactFeedbackGenerator tactile feedback iOS development