2012-05-11 13 views
26

Sto lavorando a un'app per la calcolatrice grafica per iPad e volevo aggiungere una funzione in cui un utente può toccare un'area nella vista grafico per far apparire una casella di testo che mostra le coordinate del punto toccato. Come posso ottenere un CGPoint da questo?Come ottenere un CGPoint da una posizione selezionata?

risposta

46

avete due modi ...

1.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
} 

qui, si può ottenere la posizione con il punto di vista corrente ...

2.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 
[tapRecognizer setNumberOfTapsRequired:1]; 
[tapRecognizer setDelegate:self]; 
[self.view addGestureRecognizer:tapRecognizer]; 

qui, questo codice si usa quando vuoi fare qualcosa con il tuo oggetto perticolare o sotto-vista della tua vista principale

19

Try This

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    // Get the specific point that was touched 
    CGPoint point = [touch locationInView:self.view]; 
    NSLog(@"X location: %f", point.x); 
    NSLog(@"Y Location: %f",point.y); 

} 

È possibile utilizzare "touchesEnded" se si preferisce vedere dove l'utente solleva il dito dallo schermo, invece di dove hanno toccato il basso.

3

Se si utilizza un oggetto UIGestureRecognizer o UITouch è possibile utilizzare il metodo per recuperare il locationInView:CGPoint all'interno del data vista l'utente ha toccato.

6

probabilmente è meglio e più semplice usare un UIGestureRecognizer con la vista mappa invece di provare a suddividerla sottoclasse e toccare manualmente i tocchi.

Fase 1: In primo luogo, aggiungere il sistema di riconoscimento gesto alla visualizzazione della mappa:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(tapGestureHandler:)]; 
tgr.delegate = self; //also add <UIGestureRecognizerDelegate> to @interface 
[mapView addGestureRecognizer:tgr]; 

Fase 2: Avanti, implementare shouldRecognizeSimultaneouslyWithGestureRecognizer e tornare YES in modo che il rubinetto gesto riconoscitore può funzionare allo stesso tempo, come la mappa del (altrimenti rubinetti su perni non andranno gestito automaticamente da mappa):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer 
    :(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return YES; 
} 

Fase 3: Infine, implementare il gestore gesto:

- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr 
{ 
    CGPoint touchPoint = [tgr locationInView:mapView]; 

    CLLocationCoordinate2D touchMapCoordinate 
    = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

    NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f", 
    touchMapCoordinate.latitude, touchMapCoordinate.longitude); 
} 
+1

Questa è una risposta ben completa –

0
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) { 
    print("tap working") 
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized { 
     `print(gestureRecognizer.locationInView(gestureRecognizer.view))` 
    } 
} 
3

semplicemente si vuole lanciare in un Swift 4 risposta perché l'API è molto diverso cercando.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = event?.allTouches?.first { 
     let loc:CGPoint = touch.location(in: touch.view) 
     //insert your touch based code here 
    } 
} 

O

let tapGR = UITapGestureRecognizer(target: self, action: #selector(tapped)) 
view.addGestureRecognizer(tapGR) 

@objc func tapped(gr:UITapGestureRecognizer) { 
    let loc:CGPoint = gr.location(in: gr.view) 
    //insert your touch based code here 
} 

In entrambi i casi loc conterrà il punto che è stato toccato nella vista.

Problemi correlati