SwiftUI-Haptics

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()
.haptics(onChangeOf: value, type: .soft)
or using the function programmatically
@Environment(\.hapticGenerator) var generator
⋮
generator.hapticFeedbackOccurred(type: .click)
Feedback Types
- Notification Feedback
- Impact Feedback
- Selection Feedback
- WKHaptic
Conditional Haptics
Sometimes, you just want to play haptic feedbacks only at a specific state. At this time, .haptics(when:equalsTo:type:)
plays a role.
Of course, you can create multiple haptics on a single view:
YourView()
.haptics(when: value, equalsTo: .success, type: .success)
.haptics(when: value, equalsTo: .failure, type: .error)
onAppear Haptics
You can play a one-time haptic feedback when a view appears.
Text("I love haptics.")
.triggersHapticFeedbackWhenAppear()
Swift Package Manager
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"]),
SwiftUI-Haptics
Haptics
has a set of super-light modifiers and functions that adds haptic feedbacks to any SwiftUIView
, for example, when the specific property changes or equals to a specific value, the feedback will be triggered automatically.or using the function programmatically
Feedback Types
Platforms
Conditional Haptics
Sometimes, you just want to play haptic feedbacks only at a specific state. At this time,
.haptics(when:equalsTo:type:)
plays a role.Of course, you can create multiple haptics on a single view:
onAppear Haptics
You can play a one-time haptic feedback when a view appears.
Swift Package Manager
In your
Package.swift
Swift Package Manager manifest, add the following dependency to yourdependencies
argument:Add the dependency to any targets you’ve declared in your manifest: