防止在MKMapView中取消注释选择 [英] Preventing annotation deselection in MKMapView

查看:103
本文介绍了防止在MKMapView中取消注释选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中出现一种情况,我想禁用注释取消选择(而不是选择其他选项),因此当我点击不是注释视图的任何地方时,它应该保留当前选中的视图注释按原样.如果我点击另一个注释视图,它应该选择一个注释,然后取消选择另一个注释.

I have a situation in my app where I want to disable annotation deselection (other than when selecting another), so when I tap anywhere that is not an annotation view, it should leave the currently selected annotation as is. If I tap on another annotation view, it should select that one and deselect the other.

我希望能在MKMapViewDelegate中的willDeselectAnnotationViewMKAnnotationView中的isDeselected的范围内找到一些东西,但是不幸的是没有这样的东西.我还尝试在MKMapView的自定义子类中覆盖deselectAnnotation,但是似乎水龙头触发的取消选择不会调用该功能.

I was hoping to find something along the lines of a willDeselectAnnotationView in the MKMapViewDelegate or an isDeselected in MKAnnotationView, but there's unfortunately no such thing. I also tried overriding deselectAnnotation in a custom subclass of MKMapView, but it seems the tap triggered deselect doesn't invoke that function.

是否可以在保留选择功能的同时禁用注释取消选择功能?谢谢!

Is it possible to disable annotation deselection while preserving the ability to select? Thanks!

推荐答案

我找到了一种方法!创建一个名为"allowSelectionChanges"的布尔值,我现在才具有全局名称.然后使用MKMapView的子类,并在其中使用此重写函数:

I've found a way to do it! Make a boolean named something like "allowSelectionChanges", which I just have as global for now. Then use a subclass of MKMapView with this overriden function inside:

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return allowSelectionChanges
    } 

每当您要防止注释选择和取消选择时,请将此变量切换为false.不会影响用户在地图上移动的能力!

Switch this variable to false whenever you want to prevent annotation selection and deselection. It won't affect the user's ability to move around the map!

这是一个示例,该示例可用于在您单击标注以与之交互时如何阻止该标注被取消选择.将其放在您的MKAnnotationView子类中:

Here's an example of how this can be used to stop a callout from getting deselected when you tap on it to interact with it. Put this in your MKAnnotationView subclass:

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        let rect = self.bounds
        var isInside = CGRectContainsPoint(rect, point)
        if !isInside {
            for view in self.subviews {
                isInside = CGRectContainsPoint(view.frame, point)
                if isInside {
                    allowSelectionChanges = false
                    return true
                }
            }
            allowSelectionChanges = true
        }

        return false
    }

这篇关于防止在MKMapView中取消注释选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆