EmergeTools / Pow

Delightful SwiftUI effects for your app
https://movingparts.io/pow
MIT License
3.66k stars 153 forks source link

Feature Request: More Haptic Options #12

Closed mergesort closed 1 year ago

mergesort commented 2 years ago

As mentioned in #11 I really love the .haptic ChangeEffect you've included in Pow. I have my own homegrown solution that I've been using, but actually prefer the ergonomics of your version.

One advantage of what I've built is that it provides many haptic options, allowing me to call a variety of haptic providers, such as

@Environment(\.haptics) private var haptics: Haptics
haptics.selectionGenerator.selectionChanged()

Would it be possible to configure .changeEffect(.hapticFeedback(…)) so it could take more feedback generators?

This is the complete solution that I have if it can be of any help, or provide any more context, please let me know if there's any more information that would be helpful.

Thanks a lot for the library, it's quickly become a staple in my apps!

public struct Haptics {

    public init() {}

    public let lightImpactGenerator = UIImpactFeedbackGenerator(style: .light)
    public let rigidImpactGenerator = UIImpactFeedbackGenerator(style: .rigid)
    public let softImpactGenerator = UIImpactFeedbackGenerator(style: .soft)
    public let mediumImpactGenerator = UIImpactFeedbackGenerator(style: .medium)
    public let heavyImpactGenerator = UIImpactFeedbackGenerator(style: .heavy)
    public let selectionGenerator = UISelectionFeedbackGenerator()
    public let notificationGenerator = UINotificationFeedbackGenerator()

}

private enum HapticsEnvironmentKey: EnvironmentKey {
    static let defaultValue = Haptics()
}

public extension EnvironmentValues {
    var haptics: Haptics {
        get { self[HapticsEnvironmentKey.self] }
        set { self[HapticsEnvironmentKey.self] = newValue }
    }
}