From 315b2b5a37ef42ded3d91254c1f661feb118fa50 Mon Sep 17 00:00:00 2001 From: MaximeE Date: Wed, 1 Jun 2022 14:17:27 +0200 Subject: [PATCH] 6202: Add pan gesture recognizer to handle detection of user moving across map --- .../View/LocationSharingMapView.swift | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/RiotSwiftUI/Modules/Room/LocationSharing/View/LocationSharingMapView.swift b/RiotSwiftUI/Modules/Room/LocationSharing/View/LocationSharingMapView.swift index 97836ef64..cc6c80245 100644 --- a/RiotSwiftUI/Modules/Room/LocationSharing/View/LocationSharingMapView.swift +++ b/RiotSwiftUI/Modules/Room/LocationSharing/View/LocationSharingMapView.swift @@ -65,6 +65,9 @@ struct LocationSharingMapView: UIViewRepresentable { let mapView = self.makeMapView() mapView.delegate = context.coordinator + let panGesture = UIPanGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.didPan)) + panGesture.delegate = context.coordinator + mapView.addGestureRecognizer(panGesture) return mapView } @@ -106,11 +109,12 @@ struct LocationSharingMapView: UIViewRepresentable { @available(iOS 14, *) extension LocationSharingMapView { - class Coordinator: NSObject, MGLMapViewDelegate { + class Coordinator: NSObject, MGLMapViewDelegate, UIGestureRecognizerDelegate { // MARK: - Properties var locationSharingMapView: LocationSharingMapView + var mapCenterCoordinate: CLLocationCoordinate2D? // MARK: - Setup @@ -158,13 +162,7 @@ extension LocationSharingMapView { } func mapView(_ mapView: MGLMapView, regionDidChangeAnimated animated: Bool) { - let mapCenterCoordinate = mapView.centerCoordinate - // Prevent this function to set pinLocation when the map is openning - guard let userLocation = locationSharingMapView.userLocation, - !userLocation.isEqual(to: mapCenterCoordinate, precision: 0.0000000001) else { - return - } - locationSharingMapView.mapCenterCoordinate = mapCenterCoordinate + self.mapCenterCoordinate = mapView.centerCoordinate } // MARK: Callout @@ -182,11 +180,24 @@ extension LocationSharingMapView { } func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) { - locationSharingMapView.onCalloutTap?(annotation) // Hide the callout mapView.deselectAnnotation(annotation, animated: true) } + + // MARK: UIGestureRecognizer + + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return gestureRecognizer is UIPanGestureRecognizer + } + + @objc + func didPan() { + guard let mapCenterCoordinate = mapCenterCoordinate else { + return + } + locationSharingMapView.mapCenterCoordinate = mapCenterCoordinate + } } }