如何检测没有移动或持续时间的SwiftUI touchDown事件? [英] How do you detect a SwiftUI touchDown event with no movement or duration?

查看:121
本文介绍了如何检测没有移动或持续时间的SwiftUI touchDown事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测手指何时首次与SwiftUI中的视图进行接触.我可以使用UIKit Events非常轻松地做到这一点,但是无法在SwiftUI中解决这个问题.

I'm trying to detect when a finger first makes contact with a view in SwiftUI. I could do this very easily with UIKit Events but can't figure this out in SwiftUI.

我尝试了最小移动为0的DragGesture,但直到您的手指移动它仍然不会改变.

I've tried a DragGesture with minimum movement of 0 but it still won't change until your finger moves.

TapGesture仅在您松开手指时起作用,而无论我将参数设置为什么,LongPressGesture都不会足够快地触发.

TapGesture will only work when you lift your finger and LongPressGesture will not trigger fast enough no matter what I set the parameters to.

DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged({ _ in print("down")})

LongPressGesture(minimumDuration: 0.01, maximumDistance: 100).onEnded({_ in print("down")})

我想在手指与视图接触时立即检测到touchDown事件. Apple的默认手势对距离或时间有限制.

I want to detect a touchDown event as soon as a finger makes contact with a view. Apple's default gestures have restrictions to either distance or time.

更新:这不再是问题,因为苹果似乎已经更新了DragGesture的工作方式,或者我遇到了特定的上下文错误.

Update: This is not an issue anymore as Apple has seemed to update how DragGesture works or maybe I was experiencing a specific contextual bug.

推荐答案

如果将以下两个问题中的代码组合在一起:

If you combine the code from these two questions:

如何在SwiftUI中检测点击手势位置?

UITapGestureRecognizer-使它在触地而不是触地时起作用吗?

您可以进行以下操作:

ZStack {
    Text("Test")
    TapView {
        print("Tapped")
    }
}

struct TapView: UIViewRepresentable {
    var tappedCallback: (() -> Void)

    func makeUIView(context: UIViewRepresentableContext<TapView>) -> TapView.UIViewType {
        let v = UIView(frame: .zero)
        let gesture = SingleTouchDownGestureRecognizer(target: context.coordinator,
                                                       action: #selector(Coordinator.tapped))
        v.addGestureRecognizer(gesture)
        return v
    }

    class Coordinator: NSObject {
        var tappedCallback: (() -> Void)

        init(tappedCallback: @escaping (() -> Void)) {
            self.tappedCallback = tappedCallback
        }

        @objc func tapped(gesture:UITapGestureRecognizer) {
            self.tappedCallback()
        }
    }

    func makeCoordinator() -> TapView.Coordinator {
        return Coordinator(tappedCallback:self.tappedCallback)
    }

    func updateUIView(_ uiView: UIView,
                      context: UIViewRepresentableContext<TapView>) {
    }
}

class SingleTouchDownGestureRecognizer: UIGestureRecognizer {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        if self.state == .possible {
            self.state = .recognized
        }
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
        self.state = .failed
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        self.state = .failed
    }
}

我们肯定可以做出一些抽象,以便其用法更像其他SwiftUI手势,但这只是一个开始.希望苹果在某个时候能够对此提供支持.

There's definitely some abstractions we can make so that the usage is more like the other SwiftUI Gestures, but this is a start. Hopefully Apple builds in support for this at some point.

这篇关于如何检测没有移动或持续时间的SwiftUI touchDown事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆