SimpleHaptic
is a Swift package that simplifies the implementation of haptic feedback in iOS applications. It provides an easy-to-use interface for the UIImpactFeedbackGenerator
, UINotificationFeedbackGenerator
, and UISelectionFeedbackGenerator
classes, making it straightforward to add haptic feedback to your iOS and SwiftUI apps.
- Simple and intuitive API for triggering haptic feedback.
- Supports impact, notification, and selection haptic types.
- Custom SwiftUI view modifiers for integrating haptic feedback into interactive UI components.
- iOS 14.0 or later
- Swift 5.9 or later
To integrate SimpleHaptic
into your Xcode project using Swift Package Manager, follow these steps:
- Open your project in Xcode.
- Select
File
>Swift Packages
>Add Package Dependency
. - Enter the following repository URL:
https://github.com/lukacs-m/SimpleHaptic
- Follow the prompts to complete the integration.
SimpleHaptic
provides three main types of haptic feedback, each suited for different user interaction scenarios:
Impact feedback is used to create a sense of collision or physical contact. It comes in different styles like light, medium, and heavy, which signify the intensity of the feedback.
Usage: Ideal for scenarios where the user performs an action that involves a sense of physical force or contact, like moving a slider to a specific position, or snapping an object into place.
SimpleHaptic.impact(.heavy).trigger()
Notification feedback is used to communicate success, warning, or error states to the user.
-
Success: Indicates that a task or action has been completed successfully.
-
Warning: Suggests caution or alerts the user to a potential issue.
-
Error: Indicates that an action has failed or a problem has occurred.
Usage: Perfect for scenarios like form submissions (success), password mismatch warnings (warning), or upload failures (error).
SimpleHaptic.notification(.error).trigger()
Selection feedback provides a subtle tap, giving users tactile feedback during changing selections, such as scrolling through a picker.
Usage: Best used in pickers, switches, or any elements where the user scrolls or chooses between different options.
SimpleHaptic.selection.trigger()
You can easily add haptic feedback to any SwiftUI view using the provided view modifiers.
Adding to a Button
Button(action: {
// Your action here
}) {
Text("Tap Me")
}.hapticFeedback(.impact(.medium))
Adding to a Toggle
Toggle(isOn: $isToggled) {
Text("Toggle Me")
}.hapticFeedback(.notification(.success))
SimpleHaptic is released under the MIT License.
Martin Lukacs