ios - 手势识别器并不总是被触发

标签 ios objective-c mkmapview

我正在尝试使 map 注释的标注气泡可点击(我希望标题可点击)。从我所看到的情况来看,没有什么好的方法可以做到这一点,所以我在 map 上实现了一个手势识别器,这样我就可以做一个 HitTest 来确定是否已经点击了标注。它在大多数情况下都可以正常工作,但有时手势识别器无法触发。

这是我的代码

- (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 模拟器进行测试

最佳答案

将委托(delegate)添加到 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;
}

关于ios - 手势识别器并不总是被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42679406/

相关文章:

objective-c - 自定义全屏 NSWindow 中奇怪的大小调整?

iphone - TableView 未从 NSNotification 重新加载

iphone - 检测手指是否握住物体

iOS MapKit在mapView为3D时获取MKMapView的实际可见区域

ios - UILabel 并不总是出现在它的 super View 中

ios - 在 tableview 单元格中更改 View 的颜色不会改变

iOS 代码覆盖率报告包含不应该存在的文件

ios - 如何使用 mapView 和 CLLocationManager 在 iOS Swift 中绘制两个位置之间的方向

ios - Apple Map (MKMapView) 墨卡托投影的 EPSG

ios - Apache cordova/phonegap 如何从 config.xml 启用功能