UIButton TouchUpInside Touch Ubicación


Así que tengo un UIButton grande, es un UIButtonTypeCustom, y el objetivo del botón se llama para UIControlEventTouchUpInside. Mi pregunta es cómo puedo determinar en qué parte del UIButton se produjo el contacto. Quiero esta información para poder mostrar una ventana emergente desde la ubicación táctil. Esto es lo que he intentado:

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

Y varias otras iteraciones. El método de destino del botón obtiene información de él a través del sender:

    UIButton *button = sender;

Entonces, ¿hay alguna manera de que pueda usar algo como: button.touchUpLocation?

Miré en línea y no pude encontrar nada similar a esto, así que gracias de antemano.

Author: Andrew, 2011-09-11

2 answers

UITouch *theTouch = [touches anyObject];
CGPoint where = [theTouch locationInView:self];
NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);

Esa es la idea correcta, excepto que este código está probablemente dentro de una acción en su controlador de vista, ¿verdad? Si es así, entonces self se refiere al controlador de vista y no al botón. Usted debe pasar un puntero al botón en -locationInView:.

Aquí hay una acción probada que puede probar en su controlador de vista:

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
    UIView *button = (UIView *)sender;
    UITouch *touch = [[event touchesForView:button] anyObject];
    CGPoint location = [touch locationInView:button];
    NSLog(@"Location in button: %f, %f", location.x, location.y);
}
 57
Author: Caleb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-02 14:17:16

Para Swift 3.0:

@IBAction func buyTap(_ sender: Any, forEvent event: UIEvent) 
{
       let myButton:UIButton = sender as! UIButton
       let touches: Set<UITouch>? = event.touches(for: myButton)
       let touch: UITouch? = touches?.first
       let touchPoint: CGPoint? = touch?.location(in: myButton)
       print("touchPoint\(touchPoint)")  
}
 5
Author: Ammar Mujeeb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-27 09:30:19