2

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

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.

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

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
  • 1
    Welcome to Stack Overflow! This is a [Related](https://stackoverflow.com/questions/34235903/press-and-hold-button-for-repeat-fire) question that might help, but unfortunately uses an older version of swift. You can, however check it out to see if it helps. – Mr. Xcoder Jun 27 '17 at 05:37
  • Thank you Mr. Xcoder; I did see this one and figured it was doing more than I wanted it to but I can try to implement it and see if it'll work. – Richard Haydon Jun 27 '17 at 06:16

1 Answers1

1

Here is the implementation. First, make sure that you connect the three actions of your button in this way.enter image description here

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
}
Fangming
  • 24,551
  • 6
  • 100
  • 90
  • thank you! Unfortunately, I have not been able to get this to run. I have declared holdTimer and addPercentTimer as Timer() objects. I have also created an IBOutlet for the plusButton (I don't know if this is required). I have also amended the addPercent() function as preAssessmentProbability is a Label. After all that, I'm only getting one error which is in regard to you first line: plusButton.addTarget ... for: .touchDown. That error is "Declaration Expected" which confuses me! I assumed it was declared in the IBAction or IBOutlet! Can you give me any pointers?! Thanks +++ – Richard Haydon Jun 28 '17 at 09:43
  • @RichardHaydon Hey. My methods are for adding button listeners. You are using IBActions? Meaning storyboard connections? If so I can update my answer for that – Fangming Jun 28 '17 at 09:51
  • @ FangminNing - yes, I'm using IBActions. I actually don't know what a button listener is, so I will look that up! I have only been coding since Monday! Would a button listener be better? If you could give me the solution for IBActions, though, it'd probably be easier for me. Thank you so much for your help! – Richard Haydon Jun 28 '17 at 10:13
  • @RichardHaydon Sorry, forget about listener, I am talking about IBAction. So IBAction is a connection made between storyboard action and the code. On the other hand, add target is a programmatically way of getting button click action. Let me update my answer to do storyboard IBAction – Fangming Jun 28 '17 at 10:25
  • Thanks @Fangming-Ning! I've added a single increment to TouchUpInside so it has original functionality. The only problem is that it now crashes after 18 increments at a timeInterval of 2 seconds and after 8 increments at a timeInterval of 1 second. I'm wondering if that has anything to do with the speed increase? You've got me much further than I ever did myself!!! – Richard Haydon Jun 28 '17 at 11:05
  • @RichardHaydon no problem. Don't forget to mark it as correct answer. Happy coding :) – Fangming Jun 28 '17 at 11:14