我有一个地图视图,它或多或少地添加了这样的注释:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"MKPinAnnotationView"];
annotationView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton addTarget:self
action:@selector(handleButtonAction)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = detailButton;
return annotationView;
}在iOS 7中,这会在标注的右边放置一个“i”图标。点击图标会触发mapView:annotationView:calloutAccessoryControlTapped: (在委托上)和handleButtonAction: (在self上)。不过,我最近意识到,您也可以点击其他任何地方的标注,并且会触发相同的两个方法。
这种情况发生在UIButtonTypeDetailDisclosure类型的按钮上,但似乎不会发生在UIButtonTypeCustom按钮上。当我在根本没有附件视图时点击标注时,委托方法也不会被触发。(当然,这种行为并不令人惊讶;令人惊讶的是,如果附件视图是一个细节披露按钮,那么无论您是点击按钮本身还是在标注的其他地方,这两种方法都会被触发。)
我想去掉标注中的按钮--或者至少用一个显示自己图像的按钮来替换它,而不是股票“i”图标--同时仍然允许用户点击呼号上的任何地方来触发我的操作。这个是可能的吗?我没有看到一个与“标注点击”相对应的MKMapViewDelegate方法。
发布于 2014-03-11 10:13:09
尝试在不更改UIButtonTypeDetailDisclosure类型的情况下为按钮设置自定义图像。
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];对于iOS7和更高版本,默认情况下此图像将被着色。如果要保留原始图标,请使用以下命令
[[UIImage imageNamed:@"icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]或者如果您想要删除图标
[detailButton setImage:[UIImage new] forState:UIControlStateNormal];发布于 2014-11-06 01:29:10
若要在用户单击“注释”视图后单击“标注”按钮,请在UITapGestureRecognizer中添加didSelectAnnotationView。通过这种方式,您可以在不需要附件视图的情况下实现对标注的点击。
然后,您可以从发送方获得注释对象,以便进行进一步的操作。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)];
[view addGestureRecognizer:tapGesture];
}
-(void)calloutTapped:(UITapGestureRecognizer *) sender
{
NSLog(@"Callout was tapped");
MKAnnotationView *view = (MKAnnotationView*)sender.view;
id <MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
[self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation];
}
}发布于 2016-01-30 08:23:32
Swift 3中的Dhanu A's solution:
func mapView(mapView: MKMapView, didSelectAnnotationView view:MKAnnotationView) {
let tapGesture = UITapGestureRecognizer(target:self, action:#selector(calloutTapped(sender:)))
view.addGestureRecognizer(tapGesture)
}
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
view.removeGestureRecognizer(view.gestureRecognizers!.first!)
}
func calloutTapped(sender:UITapGestureRecognizer) {
let view = sender.view as! MKAnnotationView
if let annotation = view.annotation as? MKPointAnnotation {
performSegue(withIdentifier: "annotationDetailSegue", sender: annotation)
}
}https://stackoverflow.com/questions/22133034
复制相似问题