English 中文(简体)
任何人都得t 快速倡议+MapKit+Long 新闻工作
原标题:Anybody got SwiftUI+MapKit+LongPress working?

我正试图获得<代码>。 快速倡议 +MapKit +LongPress/code> gesture working. 当我在<条码>中添加地图时,该地图发挥了巨大作用。 然后,在地图上添加<条码>。 长期的新闻工作是成功的。

我正在致力于:

Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true)
  .onLongPressGesture {
    // How do I get the location (Lat/Long) I am pressed on?
    print("onLongPressGesture")
  }

此外,是否还有办法从长篇的报摊中接手?

只使用<条码>SwiftUI,而不在<条码>内打上<条码>。

最佳回答

不要问为什么,但这似乎奏效:

    Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true)
        .gesture(DragGesture())
        .onLongPressGesture {
            print("Here!")
        }
问题回答

Yep.

缩略语

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
            // We need to update the region when the user changes it
            // otherwise when we zoom the mapview will return to its original region
            DispatchQueue.main.async {
                if self.longPress == nil {
                    let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGesture(recognizer:)))
                    mapView.addGestureRecognizer(recognizer)
                    self.longPress = recognizer
                }
                self.parent.region = mapView.region
            }
        }

之后

    @objc func longPressGesture(recognizer: UILongPressGestureRecognizer) {
        if let mapView = recognizer.view as? MKMapView {
            let touchPoint = recognizer.location(in: mapView)
            let touchMapCoordinate =  mapView.convert(touchPoint, toCoordinateFrom: mapView)
            
            // TODO: check if we already have this location and bail out
            if annotations.count > 0 {
                return
            }
            
            let annotation = Annotation(title: touchMapCoordinate.stringValue, coordinate: touchMapCoordinate)
            mapView.removeAnnotations(annotations.compactMap({ $0.pointAnnotation }))
            annotations.append(annotation)
            mapView.addAnnotation(annotation.pointAnnotation)
        }
    }

https://stackoverflow.com/a/71698741/1320010”

在这里,我如何能够找到长老会的位置,同时保持马普吉斯先发制人<代码>选取/代码”参数的功能(即在现有地图标上不许长时间):

struct MapLongPressTestView: View {
    @State private var selectedMapItemTag: String? = nil
    private let randomCoordinate = CLLocationCoordinate2D(latitude: .random(in: -90...90), longitude: .random(in: -180...180))

    var body: some View {
        MapReader { proxy in
            Map(selection: $selectedMapItemTag) {
                // Your Annotations, Markers, Shapes, & other map content

                // Selectable Test Example
                Annotation(coordinate: randomCoordinate) {
                    Image(systemName: "water.waves")
                        .foregroundStyle(.orange)
                } label: {
                    Text("Water, most likely")
                }
                .tag("example")
            }
            .gesture(DragGesture())
            .gesture(
                LongPressGesture(minimumDuration: 1, maximumDistance: 0)
                    .sequenced(before: SpatialTapGesture(coordinateSpace: .local))
                    .onEnded { value in
                        switch value {
                        case let .second(_, tapValue):
                            guard let point = tapValue?.location else {
                                print("Unable to retreive tap location from gesture data.")
                                return
                            }
                            
                            guard let coordinate = proxy.convert(point, from: .local) else {
                                print("Unable to convert local point to coordinate on map.")
                                return
                            }

                            print("Long press occured at: (coordinate)")
                        default: return
                        }
                    }
            )
        }
        .onChange(of: selectedMapItemTag) {
            print(selectedMapItemTag.map { "($0) is selected" } ?? "No selection")
        }
    }
}

或者,此处是<代码>Map的延伸,以减少碎块:

extension Map {
    func onLongPressGesture(minimumDuration: Double = 0, maximumDistance: CGFloat = 0, onTouchUp: @escaping (CGPoint) -> Void) -> some View {
        self
            .gesture(DragGesture())
            .gesture(
                LongPressGesture(minimumDuration: minimumDuration, maximumDistance: maximumDistance)
                    .sequenced(before: SpatialTapGesture(coordinateSpace: .local))
                    .onEnded { value in
                        switch value {
                        case .second(_, let tapValue):
                            guard let point = tapValue?.location else {
                                print("Unable to retreive tap location from gesture data.")
                                return
                            }

                            onTouchUp(point)
                        default: return
                        }
                    }
            )
    }
}

这样做是为了:

MapReader { proxy in
    Map(selection: $selectedMapItemTag) {
        // ...
    }
    .onLongPressGesture(minimumDuration: 1) { point in
        if let coordinate = proxy.convert(point, from: .local) {
            print("Long press occured at: (coordinate)")
        }
    }
}

SpacialTapGesture,可在Si 16+查阅。

Hope this helps!





相关问题
iphone browser moving itself

I am making iphone application with Xcode. I create a webView to load a web page. I load the following link: http://devfiles.myopera.com/articles/649/example3.html If I open that link by browser on ...

Using webcam to track hand gestures

I wanted to develop a program which uses the webcam to track four colours and which are going to be on my index finger and thumb of both my hands and according to the gestures of my hand the computer ...

Windows Tablet Event - disable Hold Through gesture

I ve been trying to implement a long-press feature on a Tablet PC (Windows 7). The problem is I don t get the MouseDown event when touching the tablet (touch and wait). I do get a MouseDown event ...

iPhone - Gestures on UIPickerView and UIWebView

I m making an iPhone app in which the user can do gestures (left and right swipes) to flick through tabs. My problem is that some of the pages have views such as the pickerview, webview, textfields ...

C# or JAVA library for gesture recognition from webcam?

I have a project for gesture recognition. I was wondering how can use C# or Java to program it? Is there any special library? Do I need programming or do I need a special device rather than a webcam?

Algorithm to emulate mouse movement as a human does?

I need to test a software that treats some mouse movements as "gestures". For such a task I need to emulate mouse movement from point A to point B, not in straight line, but as a real mouse moves - ...

Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

热门标签