我正在尝试使地图注释的标注气泡可点击(我希望标题可以点击)。从我所看到的情况来看,没有好的方法可以做到这一点,所以我在地图上实现了一个手势识别器,这样我就可以进行命中测试以确定标注是否已被点击。它大部分时间都可以正常工作,但有时手势识别器无法触发。
这是我的代码
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITapGestureRecognizer* calloutRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(calloutTapped:)];
calloutRecognizer.cancelsTouchesInView = false;
[self.mapView addGestureRecognizer:calloutRecognizer];
}
- (void)calloutTapped:(UITapGestureRecognizer *)gestureRecognizer
{
CGPoint hitPoint = [gestureRecognizer locationInView:self.mapView];
UIView *tappedView = [self.mapView hitTest:hitPoint withEvent:nil];
// This passthrough button ends up consuming events in the callout
// There seems to be no way to target it explicitly so we must check the class name of the view
if([NSStringFromClass([tappedView class]) isEqualToString: @"_MKSmallCalloutPassthroughButton"]){
if(self.mapView.selectedAnnotations.count > 0 ){
[self clinicTappedForClinic:[self getClinicForAnnotation :self.mapView.selectedAnnotations[0]]];
}
}
}
我正在使用iPhone 7模拟器进行测试
答案 0 :(得分:1)
有两种方法可以检测用户与注释视图的交互。常见的技巧是为MKAnnotationView
定义一个标注(您在点击典型地图应用中的图钉时看到的标准小弹出气泡)。然后在标准viewForAnnotation
方法中为注释创建注释视图:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
通过这样做,你会得到一个标注,但是你正在添加一个合适的配件,在我上面的例子中,它是一个披露指示器。这样,他们点击您的注释视图(在上面的示例中,地图上的图钉),他们看到标注,当他们点击该标注的正确附件(此示例中的小披露指示符)时,您的{{1调用(在我下面的例子中,对一些细节视图控制器执行segue):
calloutAccessoryControlTapped
这是小型iPhone屏幕上非常典型的用户体验。
但是,如果你不喜欢那个用户体验并且你不想要标准标注,而是你想要其他东西发生,你可以定义你的- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
// tap action
}
以便不显示标注,但是相反,你拦截它并做其他事情(例如,在iPad上映射应用程序,你可能会显示一些更复杂的弹出窗口而不是标准的标注)。例如,您可以让MKAnnotationView
不显示标注:
MKAnnotationView
但是您可以手动处理- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
annotationView.canShowCallout = NO;
return annotationView;
}
以检测用户何时点击了didSelectAnnotationView
,在此示例中显示了一个弹出框:
MKAnnotationView
如果你尝试使用UITapGestureRecognizer,那么你可以参考: How can I catch tap on MapView and then pass it to default gesture recognizers?
答案 1 :(得分:1)
将委托添加到tapGestureRecognizer:
//add <UIGestureRecognizerDelegate> to .h
//add this where you create tapGestureRecognizer...
tapGestureRecognizer.delegate = self;
// check tapGestureRecognizer working or not properly
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}