WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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!

2      

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

2      

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 ...

2      

Sadly, this doesn't work for me. It only provides the location after you release the long press.

   

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.