31 votes

Comment capturer le geste Tap sur MKMapView

J'essaye de capturer l'événement de tap sur mon MKMapView , de cette façon je peux déposer un MKPinAnnotation sur le point où l'utilisateur a tapé. Fondamentalement, j'ai une carte superposée avec MKOverlayViews (une superposition montrant un bâtiment) et je voudrais donner aux utilisateurs plus d'informations sur cette superposition quand ils tapotent dessus en déposant un MKPinAnnotaion et en montrant plus d'informations dans la légende. Je vous remercie.

59voto

Anna Points 44332

Vous pouvez utiliser un UIGestureRecognizer pour détecter les touches sur l'affichage de la carte.

Au lieu d'un seul tap, cependant, je suggère de regarder pour un double-tap (UITapGestureRecognizer) ou une pression longue (UILongPressGestureRecognizer). Un seul robinet peut interférer avec l'utilisateur essaie de appuyez sur la broche ou de la légende elle-même.

Dans l'endroit où vous configurez l'affichage de la carte (en viewDidLoad par exemple), joindre le geste de reconnaissance à la vue de la carte:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];

ou, pour utiliser un appui long:

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


Dans l' handleGesture: méthode:

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

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

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

5voto

o0oKodako0o Points 97

J'ai configuré un appui long ( UILongPressGestureRecognizer ) dans viewDidLoad: mais il détecte juste la seule touche du premier.

Où puis-je configurer un appui long pour détecter tout contact? (cela signifie que la carte est prête à chaque fois que l'utilisateur attend sur l'écran pour pousser une épingle)

La méthode viewDidLoad: !

 - (void)viewDidLoad {
    [super viewDidLoad];mapView.mapType = MKMapTypeStandard;

    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    [self.mapView addGestureRecognizer:longPressGesture];
    [longPressGesture release];

    mapAnnotations = [[NSMutableArray alloc] init];
    MyLocation *location = [[MyLocation alloc] init];
    [mapAnnotations addObject:location];

    [self gotoLocation];
    [self.mapView addAnnotations:self.mapAnnotations];
}
 

et la méthode handleLongPressGesture :

 -(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
    // This is important if you only want to receive one tap and hold event
    if (sender.state == UIGestureRecognizerStateEnded)
    {NSLog(@"Released!");
        [self.mapView removeGestureRecognizer:sender];
    }
    else
    {
        // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
        CGPoint point = [sender locationInView:self.mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        // Then all you have to do is create the annotation and add it to the map
        MyLocation *dropPin = [[MyLocation alloc] init];
        dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
        dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
//        [self.mapView addAnnotation:dropPin];
        [mapAnnotations addObject:dropPin];
        [dropPin release];
        NSLog(@"Hold!!");
        NSLog(@"Count: %d", [mapAnnotations count]);
    }   
}
 

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X