Haptics
has a set of super-light modifiers and functions that adds haptic feedbacks to any SwiftUI View
, for example, when the specific property changes or equals to a specific value, the feedback will be triggered automatically.
import SwiftUI
import Haptics
⋮
⋮
YourView()
.hapticFeedback(.selection, trigger: isSelected)
or using the function programmatically
HapticGenerator.performFeedback(.selection)
- iOS 14.0+
- watchOS 7.0+
- macOS 11.0+
start
: Indicates that an activity started. (watchOS only)stop
: Indicates that an activity stopped. (watchOS only)alignment
: Indicates the alignment of a dragged item. (macOS only)decrease
: Indicates that an important value decreased below a significant threshold. (watchOS only)increase
: Indicates that an important value increased above a significant threshold. (watchOS only)levelChange
: Indicates movement between discrete levels of pressure. (macOS only)selection
: Indicates that a UI element’s values are changing. (iOS & watchOS)success
: Indicates that a task or action has completed. (iOS & watchOS)warning
: Indicates that a task or action has produced a warning of some kind. (iOS & watchOS)error
: Indicates that an error has occurred. (iOS & watchOS)impact
: Provides a physical metaphor you can use to complement a visual experience. (iOS & watchOS)
Play haptic feedbacks when the value changes.
YourView()
.hapicFeedback(.selection, trigger: isSelected)
If the value being monitored changes, returns a HapticFeedback
to be performed.
Return nil
means DO NOT perform any haptics.
You can provide different haptic feedbacks based on your trigger value.
YourView()
.hapicFeedback(trigger: workStatus) { _, newValue in
return switch {
case .success: .success
case .failure: .error
default: nil
}
}
.hapicFeedback(.impact, trigger: cameraSession.capturedPhoto) { _, newValue in
return newValue == true // Only plays feedback when photo has been taken
}
Yeah.
If you want to use .sensoryFeedback
API but need to support older platform, SwiftUI-Haptics
is a better solution.
Replace sensoryFeedback
to hapticFeedback
.
Everything just works.
In your Package.swift
Swift Package Manager manifest, add the following dependency to your dependencies
argument:
.package(url: "https://github.com/LiYanan2004/SwiftUI-Haptics.git", .branch("main")),
Add the dependency to any targets you've declared in your manifest:
.target(name: "MyTarget", dependencies: ["Haptics"]),