gpt4 book ai didi

iphone - 如何从通过 MKAnnotationView 添加的按钮获取点击事件

转载 作者:技术小花猫 更新时间:2023-10-29 10:16:09 24 4
gpt4 key购买 nike

任何人都知道是否有办法从添加到 MKAnnotationViewbutton 获取点击事件,此 button 仅用作标签在 map 上显示每个图钉的名称,现在当 pin 被点击,所以我需要在点击按钮(标签)时做同样的事情。

感谢您提供的任何建议。

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 View 并添加一个附件按钮,如 progrmr 所示。

但是,如果您必须将按钮直接添加到 MKAnnotationView,您的方法的问题是 MKPinAnnotationView 的默认框架(不能轻易地已更改)小于您要添加的按钮,因此大多数按钮不会响应触摸,即使您切换到使用 MKAnnotationView 并增加框架大小,MKMapView 将阻止按钮获得任何触摸。

您需要做的是将 UITapGestureRecognizer 添加到按钮(使用手势处理程序的操作方法而不是按钮上的 addTarget)并将按钮添加到普通的 MKAnnotationView 使用适当的帧大小而不是 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);
}


请注意,这将阻止触发 map View 的 didSelectAnnotationView 委托(delegate)方法。如果您需要触发该方法(作为按钮的手势处理程序的补充),则添加以下内容:

//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;
}

关于iphone - 如何从通过 MKAnnotationView 添加的按钮获取点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6941199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com