gpt4 book ai didi

objective-c - MKMapView 上的 UITapGestureRecognizer 破坏了 MKAnnotation 选择

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:06 24 4
gpt4 key购买 nike

我已经将 UITapGestureRecognizer 添加到 MKMapView 中,如下所示:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(doStuff:)];
[tapGesture setCancelsTouchesInView:NO];
[tapGesture setDelaysTouchesEnded:NO];
[[self myMap] addGestureRecognizer:tapGesture];
[tapGesture release];

这几乎可以工作:识别点击手势并且双击仍然可以缩放 map 。不幸的是,UITapGestureRecognizer 会干扰 MKAnnotationView 元素的选择和取消选择,这些元素也是由点击手势触发的。

设置 setCancelsTouchesInViewsetDelaysTouchesEnded 属性没有任何效果。如果我不添加 UIGestureRecognizer,注释选择工作正常。

我错过了什么?

更新:

正如下面 Anna Karenina 所建议的,可以通过在 shouldRecognizeSimultaneouslyWithGestureRecognizer: 委托(delegate)方法中返回 YES 来避免这个问题。

更多详情请参见 this answer .

最佳答案

代替点击手势,添加长按手势如下:-

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(longpressToGetLocation:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];


- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;

CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D location =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

}

关于objective-c - MKMapView 上的 UITapGestureRecognizer 破坏了 MKAnnotation 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8783583/

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