4

我可以使用 ButtonStyle 创建我的自定义按钮样式,但我想在按钮被按下并松开时添加触觉反馈。我知道configuration.isPressedButtonStyle 中有一个可用的变量,但是当它实际发生变化时如何提供触觉反馈。

我知道如何提供触觉反馈,但需要帮助知道用户何时触摸按钮并让我们走。

我已经尝试将此扩展程序作为按钮:

struct TouchGestureViewModifier: ViewModifier {
    let touchBegan: () -> Void
    let touchEnd: (Bool) -> Void
    let abort: (Bool) -> Void

    @State private var hasBegun = false
    @State private var hasEnded = false
    @State private var hasAborted = false

    private func isTooFar(_ translation: CGSize) -> Bool {
        let distance = sqrt(pow(translation.width, 2) + pow(translation.height, 2))
        return distance >= 20.0
    }

    func body(content: Content) -> some View {
        content.gesture(DragGesture(minimumDistance: 0)
                .onChanged { event in
                    guard !self.hasEnded else { return }

                    if self.hasBegun == false {
                        self.hasBegun = true
                        self.touchBegan()
                    } else if self.isTooFar(event.translation) {
                        self.hasAborted = true
                        self.abort(true)
                    }
                }
                .onEnded { event in
                    print("ended")
                    if !self.hasEnded {
                        if self.isTooFar(event.translation) {
                            self.hasAborted = true
                            self.abort(true)
                        } else {
                            self.hasEnded = true
                            self.touchEnd(true)
                        }
                    }
                    self.hasBegun = false
                    self.hasEnded = false
                })
    }
}

// add above so we can use it
extension View {
    func onTouchGesture(touchBegan: @escaping () -> Void = {},
                        touchEnd: @escaping (Bool) -> Void = { _ in },
                        abort: @escaping (Bool) -> Void = { _ in }) -> some View {
        modifier(TouchGestureViewModifier(touchBegan: touchBegan, touchEnd: touchEnd, abort: abort))
    }
}

然后我在一个视图上这样做:

.onTouchGesture(

                // what to do when the touch began
                touchBegan: {

                    // tell the view that the button is being pressed
                    self.isPressed = true

                    // play click in sound
                    playSound(sound: "clickin-1", type: "wav")

                    // check if haptic feedback is possible
                    if self.engine != nil {
                        // if it is - give some haptic feedback
                        let pattern = try? CHHapticPattern(events: [self.event], parameters: [])
                        let player = try? self.engine?.makePlayer(with: pattern!)
                        try? self.engine!.start()
                        try? player?.start(atTime: 0)
                    }
                },

                // what to do when the touch ends. sometimes this doesnt work if you hold it too long :(
                touchEnd: { _ in

                    // tell the view that the user lifted their finger
                    self.isPressed = false


                    playSound(sound: "clickout-1", type: "wav")

                    // check if haptic feedback is possible
                    if self.engine != nil {
                        // if it is - give some haptic feedback
                        let pattern = try? CHHapticPattern(events: [self.event], parameters: [])
                        let player = try? self.engine?.makePlayer(with: pattern!)
                        try? self.engine!.start()
                        try? player?.start(atTime: 0)
                    }
                },

                // if the user drags their finger away, abort the action
                abort: { _ in
                    self.isPressed = false
                }
            )

但有时它会在中途卡住,这对用户来说不是很有用。如何使用 ButtonStyle 在更可靠的 Button 上执行此操作?

4

0 回答 0