HapticFeedback

1.0.1

Backport of SensoryFeedback API. Supports iOS 14, macOS 11, watchOS 7
dm-zharov/haptic-feedback

What's New

2024-04-03T05:08:56Z

@MainActor playback requirement for iOS only

HapticFeedback

Platforms SPM supported License

Features

Backport of SwiftUI Sensory Feedback API (iOS 17).

// Native API. Only works on iOS 17, macOS 14, watchOS 10
.sensoryFeedback(.selection, trigger: value)

// Backport. Compatible with iOS 14, macOS 11, watchOS 7
.hapticFeedback(.selection, trigger: value)

Directly play Haptic Feedback.

// Determines if device supports haptic feedback
HapticFeedback.isAvailable

// Plays selection feedback
HapticFeedback.selection.play()

// Plays impact feedback
HapticFeedback.impact(weight: .heavy, intensity: 0.5).play()

Installation

The implementation is encapsulated in a single file, so you can simply drag the HapticFeedback.swift file into your project to use it.

Requirements

  • iOS 14.0+, macCatalyst 14.0+, macOS 11.0+, watchOS 7.0+
  • Swift 5.7

Swift Package Manager

To use the HapticFeedback, add the following dependency in your Package.swift:

.package(url: "https://github.com/dm-zharov/haptic-feedback.git", from: "1.0.0")

Finally, add import HapticFeedback to your source code.

Usage

Trigger On Value Changes

The haptic feedback plays when the trigger values changes.

struct MyView: View {
    @State private var showAccessory = false

    var body: some View {
        Button("Backport") {
            showAccessory.toggle()
        }
        .hapticFeedback(.selection, trigger: showAccessory)
    }
}

Trigger With Condition Closure

For more control over when you trigger the feedback use the condition closure version of the view modifier.

.hapticFeedback(.selection, trigger: showAccessory) { oldValue, newValue in
    return newValue == true
}

Trigger With Feedback Closure

For control over what feedback plays use the feedback closure version of the view modifier.

.hapticFeedback(trigger: isFinished) { oldValue, newValue in
    return newValue ? .success : .error
}

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Knowledge

Author

Dmitriy Zharov, contact@zharov.dev

License

HapticFeedback is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 03:09:46 GMT-0900 (Hawaii-Aleutian Daylight Time)