Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton press and hold - repeat action until let go

Tags:

ios

uibutton

I want to have a certain method run and repeat itself for as long as someones finger is pressed down on a button. I want that method to stop repeating itself when the finger is not on the button anymore

Is there a way to check if the touchDown is still occurring during the method implementation? Help!

like image 769
dokun1 Avatar asked May 17 '13 18:05

dokun1


1 Answers

You can use the UIControlEventTouchDown control event to start the method running, and UIControlEventTouchUpInside, or similar, to detect when the button is no longer being "pressed".

Set up the actions to the button, e.g.:

[myButton addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

(Note the above will cause touch up inside and outside the button to invoke the endButtonTouch: method.)

Then add the startButtonTouch: and endButtonTouch methods, e.g., :

- (void)startButtonTouch:(id)sender {
    // start the process running...
}

- (void)endButtonTouch:(id)sender {
// stop the running process...
}
like image 107
bobnoble Avatar answered Sep 19 '22 05:09

bobnoble