如何使用 SwiftUI 处理屏幕触摸事件? [英] How to Handle screen touch events with SwiftUI?

查看:32
本文介绍了如何使用 SwiftUI 处理屏幕触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 UIKit 时,我在 UIView 或 UIViewController 中处理.

When working with UIKit I was handling within UIView or in a UIViewController.

func touchesBegan(_ touches: Set,事件:UIEvent?)

func touchesBegan(_ touches: Set, with event: UIEvent?)

如何使用 SwiftUI 处理触摸事件?

How to handle a touch events with SwiftUI?

推荐答案

最简单的方法是添加 DragGesture.查看 DragGesture.Value 以了解你有什么可用的信息.

Easiest thing is to add a DragGesture. Check out DragGesture.Value to get a sense of what information you have available.

Circle()
    .gesture(
        DragGesture(minimumDistance: 5, coordinateSpace: .global)
            .onChanged { value in
              self.dragLocation = value.location
            }
            .onEnded { _ in
              self.dragLocation = .zero
            }
    )

您可以使用 minimumDistance: 0 以获得立即开始更新的手势,类似于 UIKit 中的 touchesBegan(...).

You can use minimumDistance: 0 in order to have a gesture that starts updating immediately, similar to touchesBegan(...) in UIKit.

这篇关于如何使用 SwiftUI 处理屏幕触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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