检测mkmapview上不是注释的点击手势?

问题描述 投票:2回答:3

如何检测是否正在点击mapview,这不是obj-c中的注释?

objective-c mkmapview
3个回答
0
投票

试试UITapGestureRecognizer:

UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] 
   initWithTarget:self action:@selector(mapDidTap:)];
[mapView addGestureRecognizer: tapGesture];

-(void)mapDidTap:(UITapGestureRecognizer *)gestureRecognizer {
    //do something when map is tapped
}

0
投票

以下代码检测地图中的点按,并在该位置添加地图标记:

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *fingerTap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self action:@selector(handleMapFingerTap:)];
    fingerTap.numberOfTapsRequired = 1;
    fingerTap.numberOfTouchesRequired = 1;
    [self.mapView addGestureRecognizer:fingerTap];

}

- (void)handleMapFingerTap:(UIGestureRecognizer *)gestureRecognizer {

    NSLog(@"handleMapFingerTap gesture: %@", gestureRecognizer);

    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }

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

    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
    pa.coordinate = touchMapCoordinate;
    pa.title = @"Hello";
    [self.mapView addAnnotation:pa];

}

0
投票

斯威夫特4,

我通过在注释视图和地图视图中添加点击手势来修复此问题。

添加点击手势来映射

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.hideFilterView))
self.mapView.addGestureRecognizer(tapGesture)

//override "viewFor annotation:" something like this
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Pin"
        annotationView?.canShowCallout = false
    } else {
        annotationView?.annotation = annotation
    }
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.didTapPin(onMap:)))
    annotationView?.addGestureRecognizer(tapGesture)

    return annotationView
}

//then don't override 
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { }

因此,将使用轻击手势处理所有引脚选择。你可以分别检测地图和针脚点击。

© www.soinside.com 2019 - 2024. All rights reserved.