2013-03-27 9 views
5

Voglio gestire un evento si verifica quando UIButton tocco è terminato. So che UIControl ha alcuni eventi che implementano tocchi (UIControlEventTouchDown, UIControlEventTouchCancel, ecc.). Ma non riesco a catturarne nessuno eccetto lo UIControlEventTouchDown e UIControlEventTouchUpInside.Come rilevare l'evento di fine tocco per UIButton?

Il mio pulsante è una sottoview di alcuni UIView. Quel UIView ha userInteractionEnabled proprietà impostata su SI.

Cosa c'è che non va?

risposta

21

È possibile impostare 'obiettivi di azione' per il vostro tasto secondo la ControlEvents

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 

Esempio:

[yourButton addTarget:self 
      action:@selector(methodTouchDown:) 
forControlEvents:UIControlEventTouchDown]; 

[yourButton addTarget:self 
      action:@selector(methodTouchUpInside:) 
forControlEvents: UIControlEventTouchUpInside]; 

-(void)methodTouchDown:(id)sender{ 

    NSLog(@"TouchDown"); 
} 
-(void)methodTouchUpInside:(id)sender{ 

    NSLog(@"TouchUpInside"); 
} 
+0

Oh, funziona benissimo. La mia disattenzione ha giocato uno scherzo crudele con me. Grazie – Brain89

3

È necessario creare la propria classe personalizzata che estende UIButton. il tuo file di intestazione dovrebbe apparire così.

@interface customButton : UIButton 
{ 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

quindi rendere il vostro file di implementazione

+0

Sfortunatamente, il mio programmatore anziano ha proibito di creare una nuova classe. Ma anche questa soluzione funziona correttamente – Brain89

0

penso che sia più facile

UILongPressGestureRecognizer *longPressOnButton = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressOnButton:)]; 
longPressOnButton.delegate = self; 
btn.userInteractionEnabled = YES; 
[btn addGestureRecognizer:longPressOnButton]; 



- (void)longPressOnButton:(UILongPressGestureRecognizer*)gesture 
{ 
    // When you start touch the button 
    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     //start recording 
    } 
    // When you stop touch the button 
    if (gesture.state == UIGestureRecognizerStateEnded) 
    { 
     //end recording 
    } 
} 
0

Basta aggiungere IBOutlets per UIButton con Eventi TouchDown: e azione primaria Triggered:

- (IBAction)touchDown:(id)sender { 
    NSLog(@"This will trigger when button is Touched"); 
} 

- (IBAction)primaryActionTriggered:(id)sender { 
    NSLog(@"This will trigger Only when touch end within Button Boundary (not Frame)"); 
} 
0

@ Ramshad di accettato answer in Swift 3.0 sintassi utilizzando seguente metodo di UIControl classe

open func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents) 

Esempio:

myButton.addTarget(self, action: #selector(MyViewController.touchDownEvent), for: .touchDown) 
myButton.addTarget(self, action: #selector(MyViewController.touchUpEvent), for: [.touchUpInside, .touchUpOutside]) 

func touchDownEvent(_ sender: AnyObject) { 
    print("TouchDown") 
} 

func touchUpEvent(_ sender: AnyObject) { 
    print("TouchUp") 
} 
0

Swift versione 3.0:

let btn = UIButton(...) 

btn.addTarget(self, action: #selector(MyView.onTap(_:)), for: .touchUpInside) 

func onTap(_ sender: AnyObject) -> Void { 

}