Clean solution to know which MKAnnotation has been tapped?

前端 未结 2 1600
醉话见心
醉话见心 2021-02-09 20:08

Ok, so you typically have some object X you want to be annotated inside a MKMapView. You do this way:

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCo         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 20:55

    I did this and it worked alright!

    It's exactly what I need because I needed to do something when the map was tapped but letting the tap into the annotation flow normally.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease];
        g.cancelsTouchesInView = NO;
        [self.mapView addGestureRecognizer:g];
    
    }
    
    - (void) handleGesture:(UIGestureRecognizer*)g{
        if( g.state == UIGestureRecognizerStateEnded ){
            NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:self.mapView.visibleMapRect];
            for ( id annotation in visibleAnnotations.allObjects ){
                UIView *av = [self.mapView viewForAnnotation:annotation];
                CGPoint point = [g locationInView:av];
                if( [av pointInside:point withEvent:nil] ){
                    // do what you wanna do when Annotation View has been tapped!
                    return;
                }   
            }
            //do what you wanna do when map is tapped
        }
     }
    

提交回复
热议问题