Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local tap position in onTapGesture?

Tags:

swiftui

Have some rendered image and need to determine tap position relative to image. Seems to be simple, but can't find the answer

  var body: some View {
    VStack {
      Image(uiImage: drawRectangle()) // some rendered image
        .onTapGesture {
          // print(x, y)?
        }
like image 975
Igor Samtsevich Avatar asked Oct 27 '19 12:10

Igor Samtsevich


1 Answers

Found this solution:

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

setting minimumDistance: 0 will call onChanged immediately

like image 68
Igor Samtsevich Avatar answered Oct 03 '22 07:10

Igor Samtsevich