NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Getting the location in the View where the user (long) tapped/pressed

Forums > SwiftUI

I have a scrollView with an Image, which the user can pan and zoom, like in the Apple Photos App. I would like to get the location (xy) the user long pressed (being the pixel x and pixel y coordinates of the Image).

In my UIKit app this was rather simple:

///Tap and hold to add an annotation with a location on the active image map
    @objc private func tapAndHoldImage(recognizer: UILongPressGestureRecognizer) {
        if recognizer.state == .ended {
            //Get coordinates for the mapImage and make a new annotation
            guard let mapImage = selectedMapImage else { return }
            let location = recognizer.location(in: mapImageView)
            (...)            
            }
        }
    }

I can't seem to figure out how to achieve the same in SwiftUI. Any help appreciated!

   

Thanks! I need it to be a long press, but I'' go and tinkle with this a bit. Will get back here!

   

Sorry, been a while. I managed to get the location with a sequenced gesture. Though the numbers are roughly half what I expect to get back, perhaps a points vs pixel thing?

Here's the ImageZoomView

import SwiftUI

public struct MapImageZoomViewer: View {

    let image: UIImage

    @State private var scale: CGFloat = 1
    @State private var lastScale: CGFloat = 1

    @State private var offset: CGPoint = .zero
    @State private var lastTranslation: CGSize = .zero

    @State var longPressLocation = CGPoint.zero

    public init(image: UIImage) {
        self.image = image
    }

    public var body: some View {
        GeometryReader { proxy in
            ZStack {
                Image(uiImage: image)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .scaleEffect(scale)
                    .offset(x: offset.x, y: offset.y)
                    .gesture(makeDragGesture(size: proxy.size))
                    .gesture(makeMagnificationGesture(size: proxy.size))
                    .gesture(makeLongPressGestureWithDragForLocation(size: proxy.size))
                    .overlay(
                        Rectangle()
                            .foregroundColor(Color.red)
                            .frame(width: 50.0, height: 50.0)
                            .position(longPressLocation)
                            .allowsHitTesting(false)
                    )
                    .padding(40)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .edgesIgnoringSafeArea(.all)
        }
    }

    // Try and get the location where the user Long Pressed
    private func makeLongPressGestureWithDragForLocation(size: CGSize) -> some Gesture {
        LongPressGesture(minimumDuration: 1).sequenced(before: DragGesture(minimumDistance: 0, coordinateSpace: .local))
            .onEnded { value in
                switch value {
                case .second(true, let drag):
                    dPrint("Drag Location", drag?.location ?? .zero)
                    dPrint("image size", image.size)
                    longPressLocation = drag?.location ?? .zero   // capture location !!
                default:
                    break
                }
            }
    }

    private func makeMagnificationGesture(size: CGSize) -> some Gesture {
        MagnificationGesture()
            .onChanged { value in
                let delta = value / lastScale
                lastScale = value

                // To minimize jittering
                if abs(1 - delta) > 0.01 {
                    scale *= delta
                }
            }
            .onEnded { _ in
                lastScale = 1
                if scale < 1 {
                    withAnimation {
                        scale = 1
                    }
                }
                adjustMaxOffset(size: size)
            }
    }

    private func makeDragGesture(size: CGSize) -> some Gesture {
        DragGesture()
            .onChanged { value in
                let diff = CGPoint(
                    x: value.translation.width - lastTranslation.width,
                    y: value.translation.height - lastTranslation.height
                )
                offset = .init(x: offset.x + diff.x, y: offset.y + diff.y)
                lastTranslation = value.translation
            }
            .onEnded { _ in
                adjustMaxOffset(size: size)
            }
    }

    private func adjustMaxOffset(size: CGSize) {
        let maxOffsetX = (size.width * (scale - 1)) / 2
        let maxOffsetY = (size.height * (scale - 1)) / 2

        var newOffsetX = offset.x
        var newOffsetY = offset.y

        if abs(newOffsetX) > maxOffsetX {
            newOffsetX = maxOffsetX * (abs(newOffsetX) / newOffsetX)
        }
        if abs(newOffsetY) > maxOffsetY {
            newOffsetY = maxOffsetY * (abs(newOffsetY) / newOffsetY)
        }

        let newOffset = CGPoint(x: newOffsetX, y: newOffsetY)
        if newOffset != offset {
            withAnimation {
                offset = newOffset
            }
        }
        self.lastTranslation = .zero
    }
}

More exploring and thinkering to do, but it's a start ...

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.