自定义callouts part 2

前一篇是part1, part2在自定义的view上加了一些uiimageview与uibutton.请看原文。

 

Part 1 showed how to build a custom map callout that provides more content flexibility than the native callout, but maintains the expected look and behavior. In part 2 we will add a very common element of the map interface into our custom callout – the accessory button. At first glance this seems simple: just add a button to the callout. However, MapKit intercepts touch events and causes undesired callout behavior. The code used to add an accessory button is also applicable to any other button(s) or responders you may want to add to a callout, giving you the flexibility to do what you feel is best for your users.

 

Add the Button

We will begin by adding the button as we normally would, to see this behavior in action. This will be done by creating a subclass of the custom callout from part 1. Notice the attempt to call the standard callback for an accessory tap in calloutAccessoryTapped.

 

We will also implement that callback in the map view delegate. Normally a new view would be pushed on to the navigation stack at this point, but for this example it will be simpler to just display an alert.

 

 

Prevent Deselection of the Parent Annotation

If the button is tapped now, the alert will be displayed, but the callout is removed because the touch event also caused the parent annotation to be deselected just as if the button were not there. To solve this problem, we will have to disable selection changes on the parent annotation and make a small change to mapView:didDeselectAnnotationView: in the mapView delegate.

First off, we need to subclass MKPinAnnotationView (or MKAnnotationView if using a custom annotation) to add a preventSelectionChange property and override setSelected:animated:.

 

 

When the button is tapped, the callout needs to set the new preventSelectionChangeproperty to YES and set it back to NO a short time later (1 second seems to be a good delay for this call). This needs to be done before the typical touch event callbacks are invoked so we will override hitTest:withEvent:. Also, The mapView keeps track of which annotations are selected, so when selection changes on the parent are re-enabled, the map view needs to be forced to select the annotation again.

 

Even though the selection change is disabled on the parent annotation view, the map view will still invoke the delegate method mapView:didDeselectAnnotationView:. Add an additional condition to the if-statement to prevent removal when the annotation view is not allowing selection changes.

 

 

Prevent Selection of Other Annotations

With the above code, the callout now behaves as expected in most situations; however, if another annotation happens to be under the button, it will be selected. The simplest way to solve this is to disable all the annotation views on the map except the custom callout and the parent annotation. We can find all of the other annotation views by getting the subviews of the superview of the callout, and checking that they inherit from MKAnnotationView. Also, they must be re-enabled a short time later (again, a 1 second delay works well).

 

Conclusion

Now the Custom Map Callout is complete. Using the code and concepts presented in this post and Part 1, you have the tools to build callouts that fit your needs. With minor adjustments to this code, you can add multiple buttons, implement a callout with adjustable width, or change the look of the callout to match your application’s style.

You may download the full source code to see a working example.

 

 

 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值