7

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.

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • What have you tried? For instance, have you tried using a `UILongPressGestureRecognizer`, a solution proposed in many duplicate questions? Or have you considered recording the time between touch down and touch up, and deciding which logic to run depending on the time passed? – Léo Natan Nov 04 '16 at 14:23
  • Do you know about tap gesture? – User511 Nov 07 '16 at 09:19
  • Have you solved your question or should I reply? – User511 Nov 10 '16 at 06:43

4 Answers4

6

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

        }
    }
Josh Homann
  • 15,933
  • 3
  • 30
  • 33
4

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
}
Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41
3

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!

Danh Huynh
  • 2,337
  • 1
  • 15
  • 18
0

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.

Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45