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.
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.
@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.
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.
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")
}
}
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With