iPhone:在MKMapView中检测Tap

时间:2009-08-14 02:22:25

标签: ios cocoa-touch mkmapview mapkit

如何在MKMapView的实例上检测到单击?我是否必须继承MKMapView,然后覆盖touchesEnded方法?

谢谢,

-Chris

8 个答案:

答案 0 :(得分:32)

如果您只是希望在不影响地图的任何其他触摸行为的情况下获得点击手势的通知,则您需要使用UITapGestureRecognizer。它非常简单,只需输入一些这样的代码即可。

UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] 
   initWithTarget:self action:@selector(didTapMap:)];
[theMKMapView addGestureRecognizer:tapRec];
[tapRec release];

每当didTapMap收到一个点击手势时,就会调用theMKMapView,并且所有捏合和拖动手势仍然可以像以前一样工作。

答案 1 :(得分:3)

答案 2 :(得分:3)

在iOS 8上完美运作

- (void)viewDidLoad 
    {
        [super viewDidLoad];

        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.numberOfTouchesRequired = 1;
        [self.mapView addGestureRecognizer:doubleTap];

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        singleTap.numberOfTapsRequired = 1;
        singleTap.numberOfTouchesRequired = 1;
        [singleTap requireGestureRecognizerToFail: doubleTap];
        [self.mapView addGestureRecognizer:singleTap];
     }

   - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
     {
            if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
                return;
            //Do your work ...
     }

答案 3 :(得分:2)

或者根据你要做的事情,添加一个MKAnnotation(推针,带标注),这样你就可以点击 - 然后你的地图代表将收到一个事件,例如。

mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

答案 4 :(得分:0)

你现在无法拦截地图视图上的触摸,你可以尝试在那里分层不透明的视图,看看它是否接受了触摸......

答案 5 :(得分:0)

只需添加一些代码片段作为@ tt-kilew答案的插图。在我的情况下,我想在地图上指出用户自己,但不想打断他的拖动触摸。

@interface PrettyViewController () <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (assign, nonatomic) BOOL userTouchTheMap;

@end

@implementation PrettyViewController

#pragma mark - UIResponder

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    self.userTouchTheMap = [[touches anyObject].view isEqual:self.mapView];
}


#pragma mark - MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //We just positioning to user
    if (!self.userTouchTheMap) {
        CLLocationDistance radius = 5000;
        [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2*radius, 2*radius) animated:YES];
    }
}

@end

答案 6 :(得分:0)

我找不到任何工作,但我想出了这个不完美的解决方案: 在viewDidLoad

let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onMapClicked))
singleTapRecognizer.delegate = self
mapView.addGestureRecognizer(singleTapRecognizer)

代表:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return touch.view!.frame.equalTo(mapView.frame)
}

答案 7 :(得分:0)

我对swit 5.x的2美分:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let v = touch.view
        let ssv = v?.superview?.superview
        if ssv === self.mapView{
            searchBar.resignFirstResponder()
        }
    }
}

有效。但说实话,如果苹果改变观点,这可能会破坏。更好的识别器。