gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:11:09 25 4
gpt4 key购买 nike

我正在为这个问题苦苦挣扎。我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com