简体   繁体   English

在Xcode 8.3.3上的Swift 3中按住标签值增加按钮

[英]Hold down label value increment button in Swift 3 on xcode 8.3.3

I have a simple app which does some probability calculations. 我有一个简单的应用程序,可以进行一些概率计算。 I have a plus button which increments a label value 0.1% at a time. 我有一个加号按钮,可一次将标签值增加0.1%。 What I want to do is increment it faster if I hold down the plus button. 我想做的是如果按住加号按钮,则可以更快地增加它。 All the code I've searched for is for older versions of Swift or Xcode and I can't get my head around how to do it! 我搜索的所有代码都是针对较早版本的Swift或Xcode的,我不知道该怎么做!

At the moment, I have returned to a functioning app with an @IBAction func called plusButton which just adds 0.1% to a label @IBOutlet called preAssessmentProbability . 目前,我已经返回了一个功能正常的应用程序,该应用程序带有一个名为plusButton@IBAction函数,该函数仅向名为preAssessmentProbability的标签@IBOutlet添加了0.1%。

I would be most grateful if anyone could help with telling me how to keep this functionality but add the ability to hold down the plusButton to increment the preAssessmentProbability label more rapidly (bonus gratitude if you can help with telling me how to set the rate of this). 如果有人可以帮助我告诉我如何保持此功能,但又增加了按住plusButton的功能以plusButton地增加preAssessmentProbability标签的能力,我将不胜感激(如果您能帮助我告诉我如何设置此功能的速度,请多谢)。

Here is the implementation. 这是实现。 First, make sure that you connect the three actions of your button in this way. 首先,请确保以这种方式连接按钮的三个动作。 在此处输入图片说明

Then your code should look like 然后你的代码应该看起来像

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM