我如何使用UILongPressGestureRecognizer来计数长按按钮上的时间

时间:2018-11-04 21:19:15

标签: ios swift switch-statement

如何使用UILongPressGestureRecognizer计数按钮按下多长时间?我想在displayLabel.text

中打印长按计数时间

我已经尝试了尽可能多的方式。

@IBOutlet weak var buttonPressed: UIButton!
@IBOutlet weak var displayLabel: UILabel!

var buttonPressedCount : Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let longPressObj = UILongPressGestureRecognizer(target: self, action: #selector(longPressButton))
    longPressObj.minimumPressDuration = 2
    longPressObj.numberOfTouchesRequired = 1
    self.view.addGestureRecognizer(longPressObj)


    // below statement is not a right one but i tried many possiblity including this one.
    if longPressObj.minimumPressDuration == TimeInterval(2) {
        displayLabel.text = "Longpressed for 2 seconds"
    } else if longPressObj.minimumPressDuration == TimeInterval(3) {
        displayLabel.text = "3"
    } else if longPressObj.minimumPressDuration == TimeInterval(3) {
        displayLabel.text = "4"
    }


}
 @IBAction func longPressButton() {

    displayLabel.text = "Button pressed for \(buttonPressedCount)"
}

我要显示长按的未单击按钮的时间。

enter image description here

提前谢谢!

编辑:- 1.我只想显示用户进行长按时的运行持续时间。我真的很感谢实时计数 2.停止按后显示总时长也很有帮助。

https://i.stack.imgur.com/ppr0W.png

3 个答案:

答案 0 :(得分:1)

由于要在长按执行时显示运行时间,因此需要使用计时器。

长按达到.began状态时启动计时器,长按达到.ended.canceled状态时停止计时器。

您的时间应该每秒重复一次,并根据当前日期和长按开始日期之间的时差更新标签。

答案 1 :(得分:0)

为什么不使用长按手势识别器,而不要将按钮动作附加到按钮上的touchDownInside事件以及touchUpInsidetouchUpOutside事件。您的touchDownInside代码可以启动计时器,这将更新标签。 touchUpInside / touchUpOutside操作将停止计时器。

答案 2 :(得分:0)

您的目标函数应包括发送者,然后您可以获取UILongPressGestureRecognizer的状态。

这里是Apple official document

首先保存手势的开始时间,然后可以使用当前时间减去开始按下的时间,以获取状态.ended(或/和.cancelled.failed)下的持续时间

示例代码:

class ViewController: UIViewController {
    var touchBeginTime: Double = 0
    var touchCountTimer: Timer?

    override func viewDidLoad() {
        super.viewDidLoad()

        let longPressObj = UILongPressGestureRecognizer(target: self, action: #selector(longPressButton(sender:)))
        longPressObj.minimumPressDuration = 2
        longPressObj.numberOfTouchesRequired = 1
        self.view.addGestureRecognizer(longPressObj)
    }

    @IBAction func longPressButton(sender: UILongPressGestureRecognizer) {
        switch sender.state {
            case .began:
                touchCountTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (timer) in
                    print("User pressing \(Date().timeIntervalSince1970 - self.touchBeginTime) sec.")
                })
                touchBeginTime = Date().timeIntervalSince1970
                break;

            case .changed:
                //print("Long pressed changed \(Date().timeIntervalSince1970 - touchBeginTime)")
                break;

            case .ended, .cancelled, .failed:
                touchCountTimer?.invalidate()
                print("User pressed total \(Date().timeIntervalSince1970 - touchBeginTime) sec.")
                break;

            default:
                break;
        }
    }
}