How create target for UIButton with long-time press(repeating handler)?

53 Views Asked by At

I try to create UIButton, which will increase some variable with long-pressing. So, when user touch button for long time increasing handler repeats.

I have something like this:

Swift 4.0

@IBOutlet var increaseButton: UIButton!
var someVariable = 0

...

increaseButton.addTarget(self, action: #selector(increase(sender:)), for: .touchUpInside)

...

@IBAction func increase(sender: UIButton) {
    someVariable += 1
}

Thanks for all answers!

1

There are 1 best solutions below

1
Sandeep On

You can use combination of three different control events for button mainly,

.touchDown
.touchUpOutside
.touchUpInside

And track the time between touchDown and any touchup events to change your handler time.