如何从通过MKAnnotationView添加的按钮获取单击事件
任何人都知道是否有办法从按钮获得点击事件,该事件被添加到 MKAnnotationView ,这个按钮用作标签只是为了显示地图上每个图钉的名称,现在我成功显示了一个自定义 pin 时code>查看(包含图像,文本....)所以我需要做同样的事情当点击按钮(标签)时。
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.
感谢您提供任何建议。
代码按钮 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];
标准UI方法是使用callout视图和在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 来自触发的委托方法。如果您需要触发该方法(在添加到按钮的手势处理程序方法中),则添加以下内容:
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;
}