简体   繁体   中英

How to detect touch end event for UIButton?

I want to handle an event occurs when UIButton touch is ended. I know that UIControl has some events implementing touches (UIControlEventTouchDown, UIControlEventTouchCancel, etc.). But I can't catch any of them except UIControlEventTouchDown and UIControlEventTouchUpInside .

My button is a subview of some UIView. That UIView has userInteractionEnabled property set to YES .

What's wrong?

You can set 'action targets' for your button according to the ControlEvents

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

Example:

[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");
}

You need to create your own custom class that extends UIButton . your header file should look like this.

@interface customButton : UIButton
{
}

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

then make your implementation file

i think it is more easy

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
    }
}

Just add IBOutlets for UIButton with Events TouchDown: and Primary Action 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)");
}

@Ramshad's accepted answer in Swift 3.0 syntax using following method of UIControl class

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

Example:

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")
}

Swift 3.0 version:

 let btn = UIButton(...)

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM