任何人都知道是否有办法从添加到 MKAnnotationView
的 button
获取点击事件,这个 button
仅用作标签在 map
上显示每个引脚的名称,现在我成功地在 pin
被点击,所以当按钮(标签)被点击时我需要做同样的事情.
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.
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];
标准的 UI 方法是使用 callout view 并添加一个附件按钮作为 progrmr 显示.
The standard UI approach is to use the callout view and add an accessory button as progrmr shows.
但是,如果您必须将按钮直接添加到 MKAnnotationView
,那么您的方法的问题是 MKPinAnnotationView
的默认框架(这不容易更改)比您添加的按钮小,因此大多数按钮不会响应触摸,即使您切换到使用 MKAnnotationView
并增加帧大小,MKMapView
将阻止按钮获得任何触摸.
However, if you must add a button directly to the MKAnnotationView
, the problems with your approach are that the MKPinAnnotationView
'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 an MKAnnotationView
and increase the frame size, the MKMapView
will prevent the button from getting any touches.
您需要做的是将 UITapGestureRecognizer
添加到按钮(使用手势处理程序的操作方法而不是按钮上的 addTarget)并将按钮添加到普通 MKAnnotationView
具有适当的帧大小,而不是 MKPinAnnotationView
.
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 plain MKAnnotationView
with an appropriate frame size instead of an MKPinAnnotationView
.
例子:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *annView = (MKAnnotationView *)[mapView
dequeueReusableAnnotationViewWithIdentifier: @"pin"];
if (annView == nil)
{
annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"pin"] autorelease];
annView.frame = CGRectMake(0, 0, 200, 50);
UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pinButton.frame = CGRectMake(0, 0, 140, 28);
pinButton.tag = 10;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinButtonTap:)];
tap.numberOfTapsRequired = 1;
[pinButton addGestureRecognizer:tap];
[tap release];
[annView addSubview:pinButton];
}
annView.annotation = annotation;
UIButton *pb = (UIButton *)[annView viewWithTag:10];
[pb setTitle:annotation.title forState:UIControlStateNormal];
return annView;
}
- (void) handlePinButtonTap:(UITapGestureRecognizer *)gestureRecognizer
{
UIButton *btn = (UIButton *) gestureRecognizer.view;
MKAnnotationView *av = (MKAnnotationView *)[btn superview];
id<MKAnnotation> ann = av.annotation;
NSLog(@"handlePinButtonTap: ann.title=%@", ann.title);
}
请注意,这将阻止地图视图的 didSelectAnnotationView
委托方法触发.如果您需要触发该方法(在 addition 到按钮的手势处理程序方法中),则添加以下内容:
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:
//in the view controller's interface:
@interface YourVC : UIViewController <UIGestureRecognizerDelegate>
//where the UITapGestureRecognizer is created:
tap.delegate = self;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
这篇关于如何从通过 MKAnnotationView 添加的按钮获取点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!