【发布时间】: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)"
}
我想显示长按未点击按钮的时间。
提前谢谢你!
编辑:- 1.我只想在用户执行长按时显示运行持续时间。我真的很感激实时计数 2. 停止按下后显示总时长是否会有所帮助。
【问题讨论】:
-
不清楚你想要什么。您是要在用户执行长按时显示运行时长,还是要在用户停止长按后显示总时长?
-
@rmaddy:对不起!我的错。我只想在用户执行长按时显示运行持续时间。我非常感谢 displayLabel 中的实时计数
-
Edit 您的问题与您的澄清,而不是在 cmets 中发布详细信息。
标签: ios swift switch-statement