how can I distinguish whether user tapped the UIButton quickly or put and hold it in Swift?

1.3k Views Asked by At

I'm creating a camera app in swift and I have a UIButton. I want to propose two options: when user single taps the button - it takes photo and when user holds his finger on a button - it records the movie until user releases the button.

I have functions for recording and taking photo, now I need to distinguish the user action on a button.

Available actions for this button are:

enter image description here

and I tried to start recording on touch down and stop recording on touch up inside, but then I don't know where should I put the code responsible for taking photos. If I put it also in touch down then when user starts recording movie - will also take a photo, and I want to avoid it.

4

There are 4 best solutions below

0
Josh Homann On BEST ANSWER

The gesture recognizers for tap and long tap work well with each other to short this out (The tap defers firing until its sure its not a long press).

    class ViewController: UIViewController{

        @IBOutlet weak var button: UIButton!
        override func viewDidLoad() {
            super.viewDidLoad()
            button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
            let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
            longPressGestureRecognizer.minimumPressDuration = 1
            button.addGestureRecognizer(longPressGestureRecognizer)
        }

        @objc private func tap(tapGestureRecognizer: UITapGestureRecognizer) {
            print("tap")
        }
        @objc private func longPress (longPressGestureRecognizer: UILongPressGestureRecognizer) {
            if longPressGestureRecognizer.state == .began {
                print("long press began")
            }

        }
    }
0
Danh Huynh On

You can use UILongPressGestureRecognizer for record video and @IBAction - Touch Up Inside for take photo function.

Step 1: In the storyboard, create an UIButton and drag UILongPressGestureRecognizer from Object libray into this button

Step 2: In the ViewController.swift, we have this code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var button: UIButton!

    @IBAction func takePhoto(_ sender: AnyObject) {
        label.text = "Take photo"
    }

    @IBAction func recordVideo(_ sender: AnyObject) {
        label.text = "Record video"
    }
}

Step 3: Open Assistant editor and connect these @IBOutlet and @IBAction

That's it!

0
Chan Jing Hong On

For just taking a single photo, you can just connect the button to an IBAction with Touch Up Inside.

For taking a video you need to first connect the button to an IBOutlet, and then add a UILongPressGestureRecognizer to it to detect when the user long press the button.

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(gesture:)))
    longPressGesture.minimumPressDuration = 1 // Minimum duration to trigger the action
    self.button.addGestureRecognizer(longPressGesture)
}

func longPress(gesture: UILongPressGestureRecognizer) {
    if gesture.state == .began {
        print("Began")
        // Video starts recording
    } else if gesture.state == .ended {
        print("End")
        // Video stops recording
    }
}

@IBAction func takePhoto(_ sender: UIButton) {
    // Take photo
}
0
Divya Bhaloidiya On

You can use following code to give button action on long press :

I used gesture to detect the log press of the button, and it work for me.

Objective-C

UILongPressGestureRecognizer *lPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(TakePhotoLongPressAction:)];
[self.btnPhoto addGestureRecognizer:lPress];
[lPress release];


- (void)TakePhotoLongPressAction:(UILongPressGestureRecognizer*)gevent
{
    if ( gevent.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Button Long Press");
    }
}

SWIFT

// add guesture recognizer
        let lPress = UILongPressGestureRecognizer(target: self, action: #selector(takePhotoLongPressAction(_:)))
        self.button.addGestureRecognizer(lPress)



func takePhotoLongPressAction(gevent: UILongPressGestureRecognizer) {
        if gevent.state == UIGestureRecognizerState.Began {
            print("Long Press button")
        }
    }

Please check and let me know if any thing required.