Skip to content

Commit 80d7b37

Browse files
authored
[ios[ Adapt GesturesManager/onMapTap and onMapLongPress (#620)
1 parent 45132a7 commit 80d7b37

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

ios/Classes/Extensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ extension CGPoint {
374374
return ScreenCoordinate(x: x, y: y)
375375
}
376376
}
377+
378+
extension MapboxMaps.MapContentGestureContext {
379+
380+
func toFLTMapContentGestureContext() -> MapContentGestureContext {
381+
MapContentGestureContext(touchPosition: point.toFLTScreenCoordinate(), point: Point(coordinate))
382+
}
383+
}
384+
377385
extension MapboxMaps.CoordinateBoundsZoom {
378386
func toFLTCoordinateBoundsZoom() -> CoordinateBoundsZoom {
379387
return CoordinateBoundsZoom(bounds: self.bounds.toFLTCoordinateBounds(), zoom: zoom)

ios/Classes/GesturesController.swift

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,16 @@ import Foundation
22
@_spi(Experimental) import MapboxMaps
33
import Flutter
44

5-
final class GesturesController: NSObject, GesturesSettingsInterface, UIGestureRecognizerDelegate, GestureManagerDelegate {
5+
final class GesturesController: NSObject, GesturesSettingsInterface, UIGestureRecognizerDelegate {
66

7+
private var cancelables: Set<AnyCancelable> = []
78
private var onGestureListener: GestureListener?
9+
private let mapView: MapView
810

9-
func gestureManager(_ gestureManager: MapboxMaps.GestureManager, didBegin gestureType: MapboxMaps.GestureType) {}
10-
11-
func gestureManager(_ gestureManager: MapboxMaps.GestureManager, didEnd gestureType: MapboxMaps.GestureType, willAnimate: Bool) {
12-
guard gestureType == .singleTap else {
13-
return
14-
}
15-
16-
let touchPoint = gestureManager.singleTapGestureRecognizer.location(in: mapView)
17-
let point = Point(mapView.mapboxMap.coordinate(for: touchPoint))
18-
let context = MapContentGestureContext(touchPosition: touchPoint.toFLTScreenCoordinate(), point: point)
19-
20-
onGestureListener?.onTap(context: context, completion: { _ in })
11+
init(withMapView mapView: MapView) {
12+
self.mapView = mapView
2113
}
2214

23-
func gestureManager(_ gestureManager: MapboxMaps.GestureManager, didEndAnimatingFor gestureType: MapboxMaps.GestureType) {}
24-
2515
@objc private func onMapPan(_ sender: UIPanGestureRecognizer) {
2616
guard sender.state == .began || sender.state == .changed || sender.state == .ended else {
2717
return
@@ -34,16 +24,6 @@ final class GesturesController: NSObject, GesturesSettingsInterface, UIGestureRe
3424
onGestureListener?.onScroll(context: context, completion: { _ in })
3525
}
3626

37-
@objc private func onMapLongTap(_ sender: UITapGestureRecognizer) {
38-
guard sender.state == .ended else { return }
39-
40-
let touchPoint = sender.location(in: mapView)
41-
let point = Point(mapView.mapboxMap.coordinate(for: touchPoint))
42-
let context = MapContentGestureContext(touchPosition: touchPoint.toFLTScreenCoordinate(), point: point)
43-
44-
onGestureListener?.onLongTap(context: context, completion: { _ in })
45-
}
46-
4727
func updateSettings(settings: GesturesSettings) throws {
4828
if let panEnabled = settings.scrollEnabled {
4929
mapView.gestures.options.panEnabled = panEnabled
@@ -124,23 +104,23 @@ final class GesturesController: NSObject, GesturesSettingsInterface, UIGestureRe
124104

125105
func addListeners(messenger: FlutterBinaryMessenger) {
126106
removeListeners()
127-
gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(onMapLongTap))
128-
mapView.addGestureRecognizer(gestureRecognizer!)
129-
mapView.gestures.delegate = self
130-
onGestureListener = GestureListener(binaryMessenger: messenger)
131107
mapView.gestures.panGestureRecognizer.addTarget(self, action: #selector(onMapPan))
132-
}
133108

134-
func removeListeners() {
135-
if let gestureRecognizer = self.gestureRecognizer {
136-
mapView.removeGestureRecognizer(gestureRecognizer)
109+
onGestureListener = GestureListener(binaryMessenger: messenger)
110+
111+
mapView.gestures.onMapTap.observe { [weak self] context in
112+
guard let self else { return }
113+
self.onGestureListener?.onTap(context: context.toFLTMapContentGestureContext()) { _ in }
114+
}
115+
.store(in: &cancelables)
116+
mapView.gestures.onMapLongPress.observe { [weak self] context in
117+
guard let self else { return }
118+
self.onGestureListener?.onLongTap(context: context.toFLTMapContentGestureContext()) { _ in }
137119
}
120+
.store(in: &cancelables)
138121
}
139122

140-
private var mapView: MapView
141-
private var gestureRecognizer: UIGestureRecognizer?
142-
143-
init(withMapView mapView: MapView) {
144-
self.mapView = mapView
123+
func removeListeners() {
124+
cancelables = []
145125
}
146126
}

0 commit comments

Comments
 (0)