自定义 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能不是最好的解决方案,但它绝对有效。首先,我尝试了很多事情,比如观察上下文等,但我从未摆脱过崩溃,而且这看起来很麻烦。所以,这就是我所做的:
我首先指定保持警报可见的控制因素是什么。就我而言,我创建了一个自定义注释视图,每当用户单击该自定义视图上的按钮时,我希望它保持可见,甚至可能更改内容。因此,我在该自定义视图上设置了一个委托,以便我的地图可以知道何时发生变化。在我的地图视图控制器中,我捕获该消息并将类成员变量设置为 true 以表示我希望保留注释视图。
注意:这将在选择消息出现之前发生。
现在,在我的 didDeselectAnnotation 方法中,我检查布尔值。如果我想保持它可见,我选择不删除注释,重置布尔值,然后手动重新选择注释,将动画设置为“否”。这使得注释视图“保持”可见——也许是作弊,但用户看不到区别。每当该布尔值表示可以取消选择时,我只需删除注释即可,一切都很好。
因此,工作流程是这样的:
发生的操作
我希望这对其他人有帮助。我花了一段时间才弄清楚这一点,所以我希望它可以节省您的时间。
如果您找到更好的解决方案,请务必将其发布在这里!
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:
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.
NOTE: This will happen before the selection messages occur.
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:
action occurred
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!
您可以使用 hittest 来抑制标注关闭,请查看以下帖子。
检测对标注标题的点击
you can suppress callout closing by using hittest, check this following post.
Detect tap on title of callout