【问题标题】:How can i count time on long-pressed button using UILongPressGestureRecognizer如何使用 UILongPressGestureRecognizer 计算长按按钮的时间
【发布时间】:2019-04-08 07:00:20
【问题描述】:

如何使用 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)

【问题讨论】:

  • 不清楚你想要什么。您是要在用户执行长按时显示运行时长,还是要在用户停止长按后显示总时长?
  • @rmaddy:对不起!我的错。我只想在用户执行长按时显示运行持续时间。我非常感谢 displayLabel 中的实时计数
  • Edit 您的问题与您的澄清,而不是在 cmets 中发布详细信息。

标签: ios swift switch-statement


【解决方案1】:

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

当长按到达.began状态时启动定时器,当长按到达.ended.canceled状态时停止(无效)定时器。

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

【讨论】:

  • 对不起,你的回答不是很清楚,我是初学者。我尝试按照您的回答进行操作,但不确定何时提供 longPress 开始状态或 .ended 状态。
  • @KRS 将您的方法签名声明更改为 func longPressButton(_ gesture: UILongPressGestureRecognizer) { 和只是 switch gesture.state { case .began: print("began") case .ended: print("ended")}
  • @LeoDabus - 这很有帮助,也很清楚。
【解决方案2】:

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

【讨论】:

  • 好的,谢谢@Duncan
【解决方案3】:

你的目标函数应该包含sender,然后你可以得到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;
        }
    }
}

【讨论】:

  • 为什么不简单地使用日期(startDate)并获取 timeIntervalSinceNow 属性?如果你想要一个积极的结果,你可以使用Date().timeIntervalSince(startDate)
  • 此解决方案仅适用于用户在长按期间移动手指的情况。 .changed 状态仅在用户在长按期间移动手指时调用。因此,如果用户开始长按,保持不动 10 秒(例如),然后抬起手指,此代码仅显示开始和结束。
  • @LeoDabus 当然可以,但是 timeIntervalSince1970 是一个double,如果你想直接保存Date,它应该是一个对象并且会保留更多的内存。只取决于你。
  • @rmaddy 是的,如果需要实时计数,它应该添加一个计时器。
  • @SerKo 需要实时计数。这是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
相关资源
最近更新 更多