如何区分用户是快速点击了 UIButton 还是将其按住 Swift?

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

我正在 swift 中创建一个相机应用程序并且我有一个 UIButton。我想提出两个选项:当用户单击按钮时 - 它会拍照,当用户将手指放在按钮上时 - 它会录制电影直到用户释放按钮。

我有录音和拍照功能,现在我需要区分用户在按钮上的操作。

此按钮的可用操作是:

并且我尝试在touch down开始录制并在touch up inside停止录制,但是我不知道应该把负责拍照的代码放在哪里。如果我也把它放在 touch down 中,那么当用户开始录制电影时 - 也会拍照,我想避免它。

点按和长按的手势识别器可以很好地相互配合以缩短这一点(点按会延迟触发,直到确定它不是长按)。

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

        }
    }

您可以使用 UILongPressGestureRecognizer 录制视频,使用 @IBAction - Touch Up Inside 拍照功能。

第 1 步:在故事板中,创建一个 UIButton 并将 UILongPressGestureRecognizer 从对象库拖到此按钮中

第 2 步:在 ViewController.swift 中,我们有以下代码:

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

第 3 步:打开辅助编辑器并连接这些 @IBOutlet@IBAction

就是这样!

如果只拍一张照片,您只需将按钮连接到带有 Touch Up Inside 的 IBAction。

要拍摄视频,您需要先将按钮连接到 IBOutlet,然后向其添加 UILongPressGestureRecognizer 以检测用户何时长按按钮。

@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
}

您可以使用以下代码在长按时执行按钮操作:

我使用手势来检测按钮的记录按下,它对我有用。

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

如果有任何需要,请检查并告诉我。