最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin multiplatform - KMP MapKit Gesture detection - Stack Overflow

programmeradmin0浏览0评论

I am using Kotlin Multiplatform with MapKit-Compose and would like to keep track of user gestures on the map like zooming/pinching, panning, etc.

So I thought I could create the MapView and add the GestureRecognizers.

val mkMapView =
    remember {
        MKMapView().apply {
            // Init.....
            setZoomEnabled(true)
            setScrollEnabled(true)
            userInteractionEnabled = true

            setDelegate(delegate)
            delegate.addGestureRecognizer(mkMapView = this)
        }
    }
@OptIn(BetaInteropApi::class)
@ObjCAction
fun handleUserInteraction() {
    Logger.d("userZoomScrollOverride triggered")
}

@OptIn(BetaInteropApi::class)
@ObjCAction
fun handleMapPan(recognizer: UIPanGestureRecognizer) {
    Logger.d("userZoomScrollOverride handleMapPan")
}

fun addGestureRecognizer(mkMapView: MKMapView) {
    val panGesture = UIPanGestureRecognizer(target = this, action = NSSelectorFromString("handleMapPan:"))
    val pinchGesture = UIPinchGestureRecognizer(target = this, action = NSSelectorFromString("handleUserInteraction:"))
    val selector = UIGestureRecognizer(target = this, action = NSSelectorFromString("handleUserInteraction:"))
    mkMapView.addGestureRecognizer(selector)
    mkMapView.addGestureRecognizer(panGesture)
    mkMapView.addGestureRecognizer(pinchGesture)
}

Of course the handleUserInteraction/handleMapPan functions are never called. I never before tried working with NSSelectorFromString or anything like that, but it was the only thing I found on the Web.

Anybody that can point me into the right direction?

I am using Kotlin Multiplatform with MapKit-Compose and would like to keep track of user gestures on the map like zooming/pinching, panning, etc.

So I thought I could create the MapView and add the GestureRecognizers.

val mkMapView =
    remember {
        MKMapView().apply {
            // Init.....
            setZoomEnabled(true)
            setScrollEnabled(true)
            userInteractionEnabled = true

            setDelegate(delegate)
            delegate.addGestureRecognizer(mkMapView = this)
        }
    }
@OptIn(BetaInteropApi::class)
@ObjCAction
fun handleUserInteraction() {
    Logger.d("userZoomScrollOverride triggered")
}

@OptIn(BetaInteropApi::class)
@ObjCAction
fun handleMapPan(recognizer: UIPanGestureRecognizer) {
    Logger.d("userZoomScrollOverride handleMapPan")
}

fun addGestureRecognizer(mkMapView: MKMapView) {
    val panGesture = UIPanGestureRecognizer(target = this, action = NSSelectorFromString("handleMapPan:"))
    val pinchGesture = UIPinchGestureRecognizer(target = this, action = NSSelectorFromString("handleUserInteraction:"))
    val selector = UIGestureRecognizer(target = this, action = NSSelectorFromString("handleUserInteraction:"))
    mkMapView.addGestureRecognizer(selector)
    mkMapView.addGestureRecognizer(panGesture)
    mkMapView.addGestureRecognizer(pinchGesture)
}

Of course the handleUserInteraction/handleMapPan functions are never called. I never before tried working with NSSelectorFromString or anything like that, but it was the only thing I found on the Web.

Anybody that can point me into the right direction?

Share Improve this question asked Jan 31 at 15:24 Basti XBasti X 811 silver badge6 bronze badges 1
  • 1 you need shouldRecognizeSimultaneouslyWithGestureRecognizer, check out this answer – Phil Dukhov Commented Feb 3 at 1:03
Add a comment  | 

1 Answer 1

Reset to default 0

As Phil Dukhov pointed out, shouldRecognizeSimultaneouslyWithGestureRecognizer is required for the map to register these events.

Define the delegate:

class GestureDelegate :
    NSObject(),
    UIGestureRecognizerDelegateProtocol {
    override fun gestureRecognizer(
        gestureRecognizer: UIGestureRecognizer,
        shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer,
    ): Boolean {
        return true 
    }
}

Set the delegate and add the gestureRecognizer to the map:

    fun addGestureRecognizer(mkMapView: MKMapView) {
        val gestureDelegate = GestureDelegate()

        val panGesture = UIPanGestureRecognizer(target = this, action = NSSelectorFromString("handleMapPan:"))
        panGesture.delegate = gestureDelegate

        mkMapView.addGestureRecognizer(panGesture)
    }

Handle the events:

    @OptIn(BetaInteropApi::class)
    @ObjCAction
    private fun handleMapPan(recognizer: UIPanGestureRecognizer) {
        Logger.d("Handling pan event.")
    }

Thank you for the help!

发布评论

评论列表(0)

  1. 暂无评论