如何从通过 MKAnnotationView 添加的按钮获取单击事件
任何人都知道是否有办法从添加到 MKAnnotationView
的 button
获取点击事件,此 button
用作标签,只是为了显示map
上每个 pin 的名称,现在我成功地在 pin
时显示自定义 view
(其中包含图像、文本......) > 被点击所以我需要在按钮时做同样的事情(标签)被单击。
感谢您提供的任何建议。
MKAnnotationView
中 button
的代码:
UIButton * pinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 28)];
[pinButton.titleLabel setTextColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]];
[pinButton setCenter:CGPointMake(pinAnnotationView.center.x + 70, pinAnnotationView.center.y + 10)];
[pinButton addTarget:self action:@selector(pinLabelClicked) forControlEvents:UIControlEventTouchUpInside];
[pinAnnotationView addSubView:pinButton];
[pinButton setUserInteractionEnabled:YES];
Anyone know if there's a way to get click event from a button
that is added to MKAnnotationView
, this button
is used as label just to display the name of each pin on the map
, now I successd to show a custom view
(which contains image, text ....) when the pin
is clicked so i need to do the same thing when the button (label) is clicked.
Thanks for any advice you can provide.
code for button
in MKAnnotationView
:
UIButton * pinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 28)];
[pinButton.titleLabel setTextColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]];
[pinButton setCenter:CGPointMake(pinAnnotationView.center.x + 70, pinAnnotationView.center.y + 10)];
[pinButton addTarget:self action:@selector(pinLabelClicked) forControlEvents:UIControlEventTouchUpInside];
[pinAnnotationView addSubView:pinButton];
[pinButton setUserInteractionEnabled:YES];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准 UI 方法是使用标注视图并添加一个附件按钮,如 progrmr 显示的那样。
但是,如果您必须直接向
MKAnnotationView
添加按钮,则您的方法的问题在于MKPinAnnotationView
的默认框架(无法轻易更改)是小于您要添加的按钮,因此大多数按钮不会响应触摸,即使您切换到使用MKAnnotationView
并增加框架大小,MKMapView
也会防止按钮获取任何触摸。您需要做的是将
UITapGestureRecognizer
添加到按钮(使用手势处理程序的操作方法而不是按钮上的 addTarget)并将按钮添加到普通的MKAnnotationView
使用适当的帧大小而不是MKPinAnnotationView
。示例:
请注意,这将阻止触发地图视图的
didSelectAnnotationView
委托方法。如果您需要触发该方法(除了按钮的手势处理程序方法之外),请添加以下内容:The standard UI approach is to use the callout view and add an accessory button as progrmr shows.
However, if you must add a button directly to the
MKAnnotationView
, the problems with your approach are that theMKPinAnnotationView
's default frame (which can't easily be changed) is smaller than the button you're adding so most of the button will not respond to touches and even if you switch to using anMKAnnotationView
and increase the frame size, theMKMapView
will prevent the button from getting any touches.What you'll need to do is add a
UITapGestureRecognizer
to the button (use the gesture handler's action method instead of an addTarget on the button) and add the button to a plainMKAnnotationView
with an appropriate frame size instead of anMKPinAnnotationView
.Example:
Note that this will prevent the map view's
didSelectAnnotationView
delegate method from firing. If you need that method to fire (in addition to the button's gesture handler method), then add the following:我使用以下代码向标注视图(在我的应用程序中)添加了一个按钮(为了清晰起见,进行了缩减):
执行此操作后,您需要实现 MKMapViewDelegate 以在点击按钮时处理对委托的回调:
I added a button to the callout view (in my app) using this code (reduced for clarity):
After doing this you need to implement the MKMapViewDelegate to handle callback to the delegate when the button is tapped:
尝试将触摸传递给子视图:
我很惊讶你有问题,但如果你的buttonView在前面?尝试确保它是:
[self BringSubviewToFront:pinButton];
try passing the touches to the subview:
I'm surprised you have a problem though if your buttonView is in front? try making sure it is:
[self bringSubviewToFront:pinButton];