Unlock the Power of Core Haptics in SwiftUI
Haptics enhance user experience by providing tactile feedback, making interactions feel more engaging and natural. Apple’s Core Hapticsframework allows developers to create rich and customizable haptic patterns beyond the standard UIFeedbackGenerator
. In this blog, we will explore how to integrate Core Haptics into a SwiftUI app, with code examples and best practices.
What is Core Haptics?
Core Haptics provides:
- Transient haptics (short, burst-like vibrations)
- Continuous haptics (sustained vibrations that can change over time)
- Audio-haptic synchronization
- Customizable parameters (intensity, sharpness, duration)
Setting Up Core Haptics in SwiftUI
To start using Core Haptics, import the necessary framework:
import CoreHaptics
import SwiftUI
Checking Device Support
Before implementing haptics, ensure that the device supports Core Haptics:
let hapticAvailable = CHHapticEngine.capabilitiesForHardware().supportsHaptics
If supportsHaptics
is false
, fallback to UIImpactFeedbackGenerator
.
Creating a Core Haptics Engine
Core Haptics works with an engine that manages haptic patterns.
class HapticManager {
private var engine: CHHapticEngine?
init() {
guard CHHapticEngine.capabilitiesForHardware().supportsHaptics else { return }
do {
engine = try CHHapticEngine()
try engine?.start()
} catch {
print("Haptic Engine failed to start: \(error.localizedDescription)")
}
}
}
Playing a Simple Transient Haptic
A transient haptic is a short burst of feedback. Here’s how to create and play one:
func playTransientHaptic(intensity: Float = 1.0, sharpness: Float = 1.0) {
guard let engine = engine else { return }
let intensityParam = CHHapticEventParameter(parameterID: .hapticIntensity, value: intensity)
let sharpnessParam = CHHapticEventParameter(parameterID: .hapticSharpness, value: sharpness)
let event = CHHapticEvent(eventType: .hapticTransient, parameters: [intensityParam, sharpnessParam], relativeTime: 0)
do {
let pattern = try CHHapticPattern(events: [event], parameters: [])
let player = try engine.makePlayer(with: pattern)
try player.start(atTime: CHHapticTimeImmediate)
} catch {
print("Failed to play haptic: \(error.localizedDescription)")
}
}
Creating a Continuous Haptic Effect
Continuous haptics can be used for immersive feedback like dragging interactions.
func playContinuousHaptic(duration: TimeInterval, intensity: Float = 1.0, sharpness: Float = 1.0) {
guard let engine = engine else { return }
let intensityParam = CHHapticEventParameter(parameterID: .hapticIntensity, value: intensity)
let sharpnessParam = CHHapticEventParameter(parameterID: .hapticSharpness, value: sharpness)
let event = CHHapticEvent(eventType: .hapticContinuous, parameters: [intensityParam, sharpnessParam], relativeTime: 0, duration: duration)
do {
let pattern = try CHHapticPattern(events: [event], parameters: [])
let player = try engine.makePlayer(with: pattern)
try player.start(atTime: CHHapticTimeImmediate)
} catch {
print("Failed to play continuous haptic: \(error.localizedDescription)")
}
}
Using Haptics in SwiftUI
Integrate haptics into SwiftUI buttons:
struct HapticView: View {
let hapticManager = HapticManager()
var body: some View {
VStack(spacing: 20) {
Button("Play Transient Haptic") {
hapticManager.playTransientHaptic()
}
.buttonStyle(.borderedProminent)
Button("Play Continuous Haptic") {
hapticManager.playContinuousHaptic(duration: 1.0)
}
.buttonStyle(.borderedProminent)
}
.padding()
}
}
Best Practices for Using Core Haptics
- Avoid overuse — excessive haptics can feel annoying.
- Respect user preferences — check
UIAccessibility.isReduceMotionEnabled
. - Use appropriate feedback types — transient for taps, continuous for sustained interactions.
- Optimize for performance — haptic engine consumes battery, use it wisely.
- Provide fallback for unsupported devices — use
UIImpactFeedbackGenerator
.
Conclusion
Core Haptics in SwiftUI allows developers to create engaging tactile experiences that improve app interactivity. Whether it’s transient feedback for taps or continuous vibrations for immersive interactions, Core Haptics adds a new dimension to user experience.
With this guide, you now have a solid foundation to start integrating haptics into your SwiftUI apps!
Comments
Post a Comment