ios - 忽略 map 查看 :didSelectAnnotationView when long press is occuring

标签 ios annotations mkmapview mapkit

我正在为这个问题苦苦挣扎。我有一个 map View ,在一个小区域中有很多图钉。当我长按 map 时,我想忽略 map View 对注释的选择,无论我是否长按注释 View 。似乎注释是在触摸时被选择的,而不是在注释 View 内部进行触摸,这很烦人。

我在 map View 中添加了长按手势:

UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addPin:)];
longRec.cancelsTouchesInView = YES;
[self.mapView addGestureRecognizer:longRec];

当我长按注释 View 时,无法识别这一点。一旦我按下,did select 注释委托(delegate)调用就会被处理,并且长按永远不会触发。

我尝试阻止点击手势识别器,这当然不起作用,因为 map View 的手势没有委托(delegate)给我的 map View Controller ,所以这不起作用:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        return NO;
    }
    return YES;
}

我还尝试在注释 View 中添加长按手势作为一种黑客手段,但这也永远不会被触发,而且我无论如何也不喜欢这种策略。

当长按手势在 map View 上待处理时,是否有办法阻止 map View 的注释选择?

最佳答案

想出了一个解决办法。基本上是MKMapView的子类化并实现handleTap和handleLongPress。

在那里,我在长按期间阻止了点击。我还给出了一点延迟来处理同时识别两个手势的情况:

@implementation KWMapView // :MKMapView

- (void)handleTap:(UITapGestureRecognizer *)tapGesture
{
    CGPoint tapPoint = [tapGesture locationInView:self];
    NSUInteger numberOfTouches = [tapGesture numberOfTouches];

    if (numberOfTouches == 1 && tapGesture.state == UIGestureRecognizerStateEnded) {
        if (!self.blockTap) {
            id v = [self hitTest:tapPoint withEvent:nil];

            if ([v isKindOfClass:[MKAnnotationView class]]) {
                [self addAnnotation:[v annotation]];
                [self selectAnnotation:[v annotation] animated:YES];
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:PNMapViewDidTapMap object:tapGesture];
            }
        }
    }
}

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
    self.blockTap = YES;

    if (sender.state == UIGestureRecognizerStateBegan) {
        [[NSNotificationCenter defaultCenter] postNotificationName:PNMapViewDropPinGesture object:sender];
    }

    if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed) {
        double delayInSeconds = .2;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            self.blockTap = NO;
        });
    }
}
@end

关于ios - 忽略 map 查看 :didSelectAnnotationView when long press is occuring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21107154/

相关文章:

swift - mapView中firebase的下载地址

ios - 有什么方法可以在不显示 ios 中的共享对话框的情况下在 facebook 上共享

annotations - PHP-DI 注释不起作用

java - Spring @Autowired @Lazy

java - 在 Spring Mvc 中配置注解

ios - MKCoordinateRegionMakeWithDistance 抛出 "Thread 1: signal SIGABRT"

ios - NSFetchRequest 从 Core Data iOS 返回错误

ios - 使用 NSURLSessionDownloadTask 在 UITableView 中下载图像的堆分配激增

iphone - 未找到 Apple Mach-O 链接器警告目录

swift - 如何使用 detailCalloutAccessoryView 更改标注气泡的默认背景颜色