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