iPhone: Rilevamento Tap in MKMapView

voti
19

Come faccio a rilevare un singolo tocco in un'istanza di MKMapView? Devo sottoclasse MKMapViewe quindi eseguire l'override del touchesEndedmetodo?

Grazie,

-Chris

È pubblicato 14/08/2009 alle 03:22
fonte dall'utente
In altre lingue...                            


8 risposte

voti
31

Se stai solo cercando di ricevere la notifica di gesti tap senza influenzare qualsiasi altro comportamento tocco della mappa, ti consigliamo di utilizzare un UITapGestureRecognizer. E 'super semplice, basta mettere in qualche codice come questo.

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

Che chiamerà il didTapMapogni volta che theMKMapViewriceve un gesto rubinetto e tutto il pizzicare, e gesti trascinando sarà ancora lavorare come hanno fatto prima.

Risposto il 31/12/2010 a 21:30
fonte dall'utente

Risposto il 10/11/2009 a 13:27
fonte dall'utente

voti
2

Perfettamente funzionante su 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 ...
     }
Risposto il 03/04/2015 a 10:46
fonte dall'utente

voti
2

O a seconda di ciò che si sta cercando di fare, aggiungere un MKAnnotation(puntina, con un sottotitolo), in modo da avere qualcosa da toccare - e poi la vostra mappa delegato riceverà un esempio evento.

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

Risposto il 18/09/2009 a 09:31
fonte dall'utente

voti
0

i miei 2 centesimi per Swit 5.x:

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()
        }
    }
}

Funziona. ma onestamente può rompere se Apple cambia strati di vista. Meglio un riconoscitore.

Risposto il 16/09/2019 a 19:19
fonte dall'utente

voti
0

Niente che io abbia mai trovato lavorato, ma mi si avvicinò con questa soluzione unperfect: In viewDidLoad

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

In delegato:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return touch.view!.frame.equalTo(mapView.frame)
}
Risposto il 20/09/2017 a 09:34
fonte dall'utente

voti
0

Basta aggiungere qualche frammento di codice come illustrazione di risposta @ tt-kilew. Nel mio caso, voglio sottolineare l'utente a se stesso sulla mappa ma non voglio interrompere il suo tocco di trascinamento.

@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
Risposto il 07/04/2017 a 13:59
fonte dall'utente

voti
0

Non potete in questo momento intercetta tocca su una visualizzazione della mappa, si può provare stratificazione una visione opaca in là e vedere se si prende tocchi ...

Risposto il 14/08/2009 a 03:59
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more