3

我已将 a 添加UITapGestureRecognizer到 an MKMapView,如下所示:

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

这几乎可行:点击手势被识别,双击仍然缩放地图。不幸的是,这UITapGestureRecognizer会干扰MKAnnotationView元素的选择和取消选择,这也是由点击手势触发的。

设置setCancelsTouchesInViewandsetDelaysTouchesEnded属性没有任何效果。如果我不添加UIGestureRecognizer.

我错过了什么?

更新:

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

此答案中的更多详细信息。

4

1 回答 1

0

而不是点击手势,添加长按手势如下: -

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);

}
于 2013-02-12T09:16:45.093 回答