我试图使 map 注释的标注气泡可点击(我希望标题可点击)。从我所看到的情况来看,没有很好的方法可以做到这一点,因此我在 map 上实现了手势识别器,以便可以进行点击测试来确定是否已点击标注。在大多数情况下,它都能正常工作,但有时手势识别器无法触发。
这是我的代码
- (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模拟器进行测试
最佳答案
将委托添加到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;
}