Sul*_*ana 14 iphone objective-c uibutton mkmapview ios
任何人都知道是否有办法从button添加的点击事件中获取MKAnnotationView,这button被用作标签只是为了显示每个图钉的名称map,现在我成功地显示了一个自定义view(包含图像,文本.... )pin单击时,我需要在单击按钮(标签)时执行相同的操作.
感谢您提供的任何建议.
为代码button在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];
Run Code Online (Sandbox Code Playgroud)
小智 16
标准UI方法是使用callout视图并添加附件按钮作为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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,这将阻止映射视图的didSelectAnnotationView委托方法触发.如果您需要触发该方法(除了按钮的手势处理程序方法),请添加以下内容:
//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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15499 次 |
| 最近记录: |