HapticManager Documentation
The HapticManager enum is a utility for managing haptic feedback on devices that support Core Haptics. It provides various methods to trigger different types of haptic feedback patterns. Below is a detailed explanation of each haptic mode and the corresponding algorithm used to create the vibration patterns.
Enum Definition
high-level structure
enum HapticManager {
static nonisolated(unsafe) private var engine: CHHapticEngine?
static func prepareHaptics()
static private func restartEngine()
static func triggerNormalHaptic()
static func triggerMultiPattern(frequency: Double, amplitude: Double) async
static func triggerGradualHaptic(intensity: Float = 0.6)
static func triggerHeavyHaptic()
}
Methods
prepareHaptics()
This method initializes the haptic engine if the device supports haptics. It starts the engine and prints a message indicating the engine has started.
restartEngine()
This private method attempts to restart the haptic engine if it fails during operation.
triggerNormalHaptic()
This method triggers a single, short haptic tap. It creates a transient haptic event with moderate intensity and low sharpness.
Algorithm:
- Check if the haptic engine is available.
- Create haptic event parameters for intensity (0.4) and sharpness (0.2).
- Create a transient haptic event with the parameters.
- Create a haptic pattern with the event.
- Create a player for the pattern and start it.
triggerMultiPattern(frequency: Double, amplitude: Double) async
This method triggers a haptic pattern based on the provided frequency and amplitude. It differentiates between low and high frequencies to create continuous or transient patterns.
Note: This Pattern is designed for Feel Your Vibe game specifically.
Algorithm:
- Check if the haptic engine is available.
- For low frequencies (<= 2.0):
- Create continuous haptic event parameters for intensity (amplitude * 0.7) and sharpness (0.3).
- Create a continuous haptic event with a duration of 0.2 seconds.
- For high frequencies (> 2.0):
- Create transient haptic event parameters for intensity (amplitude * 0.6) and sharpness (0.5).
- Create a transient haptic event.
- Create a haptic pattern with the event.
- Create a player for the pattern and start it.
triggerGradualHaptic(intensity: Float = 0.6)
This method triggers a haptic pattern that gradually increases and then decreases in intensity over a duration of 2 seconds.
Algorithm:
- Check if the device supports haptics and if the haptic engine is available.
- Create a series of continuous haptic events with varying intensity to form a bell curve.
- Each event has a duration of 0.1 seconds and the intensity peaks in the middle.
- Create a haptic pattern with the events.
- Create a player for the pattern, start the engine, and start the player.
triggerHeavyHaptic()
This method triggers a haptic pattern that starts with strong intensity and gradually decreases.
Algorithm:
- Check if the haptic engine is available.
- Create a series of continuous haptic events with decreasing intensity.
- Each event has a duration of 0.2 seconds.
- Create a haptic pattern with the events.
- Create a player for the pattern and start it.
