ver uso tengo puedo porque periodo para nada moviles los lleno limitar funciona datos consumo configurar como aplicaciones almacenamiento agarra actual activar iphone uiview uitouch

uso - no puedo activar datos moviles para aplicaciones iphone



Detectar si cierta UIView fue tocada entre otras UIViews (7)

Tengo 3 UIViews, en capas encima de una uiview grande. Quiero saber si el usuario toca el de arriba y no se preocupa por los demás. Tendré un par de botones en la segunda UIView y una UITable en la tercera UIView.

El problema es que enciendo userInteractionEngabled en la primera vista y eso funciona, pero todas las demás vistas responden de la misma manera, incluso si lo desactivo. Si deshabilito userInteractionEnabled on self.view, ninguno de ellos responde. Tampoco puedo detectar qué vista se tocó en el método delegado de touchesBegan.

Mi código:

UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)]; aView = userInteractionEnabled = YES; [self.view addSubview:aView]; UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 50)]; bView.userInteractionEnabled = NO; [self.view addSubview:bView]; -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //This gets called for a touch anywhere }


En touches Comenzar a verificar que la vista tocada sea la que desea, resolví el problema cuando intenté identificar si el usuario tocó un ImageView específico.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([touch view] == imageView ) { ... your code to handle the touch } ...

Si ajusta el contenido para las diámetros de la vista, asegúrese de cambiar el tamaño de la vista en consecuencia, de lo contrario reconocerá el evento táctil incluso en el área donde hay contenido en la vista. En mi caso, cuando intenté mantener la proporción de la imagen con UIViewContentModeScaleAspectFit, aunque la imagen se ajustó según lo requerido en las áreas de "espacios en blanco", el evento táctil también se capturó.


En mi caso, la prueba UIView que quería encontrar no fue devuelta por el hitTest. Pero el siguiente código funcionó mejor:

CGPoint locationPoint = [[touches anyObject] locationInView:self.view]; CGPoint viewPoint = [myImage convertPoint:locationPoint fromView:self.view]; if ([myImage pointInside:viewPoint withEvent:event]) { do something }


Esto funcionó para mí:

func respondToGesture(gesture: UIGestureRecognizer) { let containsPoint = CGRectContainsPoint(targetView.bounds, gesture.locationInView(targetView)) }


Esto también se puede hacer de la siguiente manera.

Primero, busca la ubicación del toque en la vista que te interesa:

CGPoint location = [touches.anyObject locationInView:viewYouCareAbout];

Luego vea si el punto está dentro de los límites de la vista:

BOOL withinBounds = CGRectContainsPoint(viewYouCareAbout.bounds, location);

Entonces, si está dentro de los límites, realiza tu acción.

Esto se puede hacer con una declaración, que por ejemplo, se puede usar directamente dentro de una declaración if:

CGRectContainsPoint(viewYouCareAbout.bounds, [touches.anyObject locationInView:viewYouCareAbout])


Mi versión rápida para comprobar que el toque tuvo lugar en infoView:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { guard let touch = touches.first, infoView.bounds.contains(touch.location(in: infoView)) else { return } print("touchesBegan") // Replace by your own code } // touchesBegan


Para verificar si se tocó cierta vista dentro de otra vista, puede usar hitTest.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;

En su implementación personalizada de touches, comience a comprobar cada toque en el conjunto de toques. El punto para el método hitTest se puede obtener usando

- (CGPoint)locationInView:(UIView *)view;

Método, donde la vista es su superView (la que contiene otras vistas).

EDIT: Aquí hay una implementación rápida y personalizada:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint locationPoint = [[touches anyObject] locationInView:self]; UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event]; }

Espero que esto haya sido útil, Paul.


código SWIFT:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { if touch.view == self.view { self.hideInView() } else { return } } }