[英]Hold down label value increment button in Swift 3 on xcode 8.3.3
我有一个简单的应用程序,可以进行一些概率计算。 我有一个加号按钮,可一次将标签值增加0.1%。 我想做的是如果按住加号按钮,则可以更快地增加它。 我搜索的所有代码都是针对较早版本的Swift或Xcode的,我不知道该怎么做!
目前,我已经返回了一个功能正常的应用程序,该应用程序带有一个名为plusButton
的@IBAction
函数,该函数仅向名为preAssessmentProbability
的标签@IBOutlet
添加了0.1%。
如果有人可以帮助我告诉我如何保持此功能,但又增加了按住plusButton
的功能以plusButton
地增加preAssessmentProbability
标签的能力,我将不胜感激(如果您能帮助我告诉我如何设置此功能的速度,请多谢)。
然后你的代码应该看起来像
@IBAction func buttonUpInside(_ sender: Any) {
self.buttonUp()
}
@IBAction func buttonUpOutside(_ sender: Any) {
self.buttonUp()
}
@IBAction func buttonTouchDown(_ sender: Any) {
self.buttonDown()
}
func buttonDown() {
increaseSpeed()
holdTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector:#selector(increaseSpeed), userInfo: nil, repeats: true)
}
func buttonUp() {
speed = 1.0
holdTimer.invalidate()
addPercentTimer.invalidate()
}
func increaseSpeed(){
if speed != 1 {
addPercentTimer.invalidate()
}
if speed > 0 {
speed -= 0.2 //make it faster!
}
addPercentTimer = Timer.scheduledTimer(timeInterval: speed, target: self, selector:#selector(addPercent), userInfo: nil, repeats: true)
}
func addPercent(){
preAssessmentProbability += 0.1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.