ios xcode ipad touch cgpoint

ios - ¿Cómo obtener un CGPoint desde una ubicación girada?



xcode ipad (6)

Estoy trabajando en una aplicación de calculadora gráfica para el iPad, y quería agregar una función en la que un usuario pueda tocar un área en la vista de gráfico para hacer aparecer un cuadro de texto que muestra la coordenada del punto que tocaron. ¿Cómo puedo obtener un CGPoint de esto?


Prueba esto

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

Puede usar "toques encerrados" si prefiere ver dónde el usuario levantó el dedo de la pantalla en lugar de dónde tocaron.


Si usa un objeto UIGestureRecognizer o UITouch , puede usar el método locationInView: para recuperar el CGPoint dentro de la vista dada que tocó el usuario.


Solo quiero arrojar una respuesta de Swift 4 porque la API es bastante diferente.

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 }

En ambos casos, loc contendrá el punto tocado en la vista.


probablemente sea mejor y más simple usar un UIGestureRecognizer con la vista de mapa en lugar de tratar de subclasificarlo e interceptar toques manualmente.

Paso 1: Primero, agrega el reconocedor de gestos a la vista del mapa:

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

Paso 2: A continuación, implemente shouldRecognizeSimultaneouslyWithGestureRecognizer y devuelva YES para que su reconocedor de toques pueda funcionar al mismo tiempo que el mapa (de lo contrario, los toques en los pins no serán manejados automáticamente por el mapa):

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

Paso 3: Finalmente, implementa el controlador de gestos:

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


tienes dos caminos ...

1.

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

aquí, puedes obtener ubicación con un punto desde la vista actual ...

2.

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

aquí, use este código cuando quiera hacer algo con su objeto particular o subvista de su vista principal


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