Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 503
user3766930 Avatar asked Nov 02 '16 00:11

user3766930


People also ask

How can I check if UIButton is pressed?

You can use the following code. class ViewController: UIViewController { var gameTimer: Timer! @IBAction func btnClick(_ sender: Any) { let button = UIButton() if (button. isSelected) { print(" Not Selected"); } else { print(" Selected"); } } override func viewDidLoad() { super.

What does the @IBAction tag do?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.

What is IBAction in Swift?

Swift code is associated with graphical interface elements through the use of outlets and actions. An IB Outlet (short for Interface Builder outlet) is a graphical component that your code links to. An IB action is the reverse: It is a method in your code that a graphical component links to.


2 Answers

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

        }
    }
like image 196
Josh Homann Avatar answered Sep 23 '22 08:09

Josh Homann


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!

like image 36
Danh Huynh Avatar answered Sep 21 '22 08:09

Danh Huynh