Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hallee/simple-haptics
👇 Core Haptics convenience wrapper for SwiftUI to make haptic taps simple
https://github.com/hallee/simple-haptics
combine corehaptics haptic-feedback haptics swift swiftui uifeedbackgenerator uikit
Last synced: 2 months ago
JSON representation
👇 Core Haptics convenience wrapper for SwiftUI to make haptic taps simple
- Host: GitHub
- URL: https://github.com/hallee/simple-haptics
- Owner: hallee
- Created: 2019-12-01T19:20:35.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-28T01:28:40.000Z (about 5 years ago)
- Last Synced: 2024-03-14T22:09:35.125Z (11 months ago)
- Topics: combine, corehaptics, haptic-feedback, haptics, swift, swiftui, uifeedbackgenerator, uikit
- Language: Swift
- Homepage:
- Size: 7.81 KB
- Stars: 16
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple Haptics
Simple Haptics is a convenience wrapper for [`Core Haptics`](https://developer.apple.com/documentation/corehaptics) to make it easier to fire transient haptic taps, similar to `UIFeedbackGenerator`.
## Usage
```swift
import SimpleHapticslet haptics = SimpleHapticGenerator()
try? haptics.fire(intensity: 0.5, sharpness: 1)
```### Integration with SwiftUI
Simple Haptics works well when integrated with `@Environment`. You can create a single `SimpleHapticGenerator` and pass it as an environment object to your root SwiftUI view, then use it in any child views that need haptic feedback. This prevents needing to initialize lots of `CHHapticEngine`s unecessarily, and allows you to stop and start the haptic engine on app lifecycle events. For example, in your `SceneDelegate`:
```swift
import SimpleHaptics
import SwiftUIclass SceneDelegate: UIResponder, UIWindowSceneDelegate {
let haptics = SimpleHapticGenerator()
...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
.environmentObject(haptics) // Pass your haptic generator into your root View....
// Be sure to stop the haptic generator when entering the background.
func sceneDidEnterBackground(_ scene: UIScene) {
haptics.stop()
}func sceneWillEnterForeground(_ scene: UIScene) {
try? haptics.start()
}}
```And then child views can use it simply by creating an `@EnvironmentObject` variable of the correct type:
```swift
import SimpleHaptics
import SwiftUIstruct MyView: View {
@EnvironmentObject private var haptics: SimpleHapticGenerator
...
}
```## Installation
#### SwiftPM
```swift
dependencies: [
.package(url: "https://github.com/hallee/simple-haptics", from: "0.0.2")
],
targets: [
.target(name: "YourTarget", dependencies: ["SimpleHaptics"])
]
```