2010-12-22 13 views

Respuesta

50

Agregar controladores de eventos al botón, algo así como lo siguiente,

[myButton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown]; 
[myButton addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside]; 
[myButton addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

controlar los eventos como los siguientes, se puede obtener el punto de contacto del evento ev como abajo,

- (void)dragBegan:(UIControl *)c withEvent:ev { 
    NSLog(@"dragBegan......"); 
    UITouch *touch = [[ev allTouches] anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 
    NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y); 
} 

- (void)dragMoving:(UIControl *)c withEvent:ev { 
    NSLog(@"dragMoving......"); 
} 

- (void)dragEnded:(UIControl *)c withEvent:ev { 
    NSLog(@"dragEnded......"); 
} 

Esta no es mi propia respuesta. Recibí este código de http://sree.cc/iphone/handling-touche-events-for-uibuttons-in-iphone e hice algunos cambios menores.

+0

¡Esto funcionó muy bien! ¡¡Muchas gracias!! –

0

Intente anular la clase UIButton y en el método touchesBegan llame a [super touchesBegan ..].

Cuestiones relacionadas