自定义MKAnnotationView-如何捕获触摸而不关闭标注? [英] Custom MKAnnotationView - How to capture touches and NOT dismiss the callout?

查看:117
本文介绍了自定义MKAnnotationView-如何捕获触摸而不关闭标注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的MKAnnotationView子类.它完全按照我的意愿显示视图.在这种情况下,我有一个按钮.我想捕获按钮上的事件以执行操作.这样就可以了.

I have a custom MKAnnotationView subclass. It is showing the view exactly as I want it to. In that view, I have a button. I want to capture events on the button to perform an action. This works just fine.

但是,我不希望标注消失或消失.基本上,触摸标注中的按钮将开始播放声音,但是我想保留注释,以便用户可以按需要停止,而无需再次触摸地图图钉以恢复注释.在另一种情况下,我希望按钮触摸可以为标注中的更多细节设置动画,因此我绝对不想在此时取消标注.

However, I do NOT want the callout to be dismissed or disappear. Basically, touching the button in the callout will start playing a sound, but I want to leave the annotation up so the user can press stop if they want to, without having to touch the map pin again to bring the annotation back up. In another instance, I want the button touch to animate more details in the callout, so I definitely don't want to dismiss the callout at that point.

每当用户选择标注或标注内的按钮时,如何防止标注消失?

How can I keep the callout from disappearing whenever the user selects the callout or a button inside the callout?

推荐答案

这可能不是最佳解决方案,但绝对可以.首先,我尝试了很多方法,例如观察上下文等等,但是我从没有崩溃过,而且看起来很麻烦.所以,这就是我所做的:

This may not be the best solution, but it definitely works. First off, I tried a number of things, like observing for context and such, but I never got past crashing, and it seemed cumbersome. So, this is what I did:

我首先指定了使警报可见的控制因素.就我而言,我创建了一个自定义注释视图,每当用户单击该自定义视图上的按钮时,我都希望它保持可见状态,甚至更改内容.因此,我在该自定义视图上设置了一个委托,以便我的地图可以知道何时发生了更改.在我的地图视图控制器中,我捕获了该消息,并将类成员变量设置为true,以表示我希望注释视图保持不变.

I first specified what the controlling factor was for keeping an alert viewable. In my case, I created a custom annotation view, and whenever the user clicks a button on that custom view, I want it to stay visible, and maybe even change the content. So, I set a delegate on that custom view so that my map can know when something changes. In my map view controller, I catch that message and set a class member variable to true to signify that I want the annotation view to stay.

注意:此将在选择消息出现之前发生.

现在,在我的didDeselectAnnotation方法中,我检查了布尔值.如果要使其可见,我选择不删除注释,重置布尔值,然后手动重新选择注释,将动画设置为否".这使注释视图保持"可见-可能是作弊,但用户看不到区别.每当该布尔值表示取消选择就可以时,我只需删除注释即可,一切都很好.

Now, in my didDeselectAnnotation method, I check the boolean value. If I want to keep it visible, I opt to NOT remove my annotation, I reset the boolean value, and I re-select the annotation manually, setting animation to NO. This lets the annotation view "stay" visible--maybe a cheat, but the user can't see the difference. Whenever that boolean value says that deselection is ok, I simply remove the annotation and all is well.

所以,工作流程是这样的:

So, the workflow is this:

  1. 触摸针
  2. 显示了CustomAnnotationView
  3. 用户单击CustomAnnotationView上的按钮,该按钮通知委托(mapView) 发生了动作
  4. 设置类的布尔值以知道您要保留注释
  5. mapView然后调用didDeselectAnnotation方法
  6. 在didDeselectAnnotation方法中,使用条件语句来决定是否应删除注释,或者通过不删除注释并在不使用动画的情况下手动重新选择它来保留注释.
  1. Touch pin
  2. CustomAnnotationView is displayed
  3. User clicks a button on CustomAnnotationView, which notifies the delegate (mapView) that the action occurred
  4. Set the class boolean value to know that you want to keep the annotation around
  5. mapView then calls didDeselectAnnotation method
  6. In didDeselectAnnotation method, use conditional to decide if you should remove the annotation, or keep it around by not removing the annotation and manually re-selecting it without animation.

我希望这对其他人有帮助.我花了一些时间才弄清楚这一点,所以我希望它可以节省您的时间.

I hope this helps others. It took me a while to figure this out, so I hope it saves you time.

无论如何,如果找到更好的解决方案,请在此处发布!

If you find a better solution, by all means, please post it here!

这篇关于自定义MKAnnotationView-如何捕获触摸而不关闭标注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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