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

Ado*_*els 4 ios swiftui

使用UIKit时,我在UIView或UIViewController中进行处理。

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

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

ars*_*ius 7

最简单的方法是添加一个DragGesture。 签出 DragGesture.Value了解您所拥有的信息。

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

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