Is it possible to differentiate between a long press and a tap on a button?

后端 未结 3 437
耶瑟儿~
耶瑟儿~ 2020-12-29 10:02

Can we call different actions / delegates in response to the two different events of

  1. A tap on a UIButton
  2. A tap-and-hold on a UIButton

3条回答
  •  孤城傲影
    2020-12-29 10:46

    The best solution I can think of, is to create another class, and subclass UIButton. Then on Interface Builder (if that's what you're using), you can set the button's class to the custom class you just created.

    So in this new class, you have to override a method called

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    

    This is basically telling you that someone pressed down on your button. The touches is an NSSet and it holds all the information for all the fingers that are pressing down on the screen. If you only are interested in the one that's pressing on the button itself, you'll probably have something like:

    NSSet *myTouches = [event touchesForView:self.view];
    

    So now that you have the touches that correspond to your button, you have to find out how many times the user tapped on that button. You do that with something like:

    NSUInteger numTaps = [[myTouches anyObject] tapCount];
    

    If this number is 2, that means the user just double tapped your button. Now comes the next part. How do you know if the user is holding that button? Well when the user lets go of the screen, another method gets called. You'll need to override that one as well:

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    

    This is where you know if the person has stopped touching the screen or if his finger is still on it. If his finger is still on it, then this event hasn't been called yet.

    Now enough with the background

    Here's my suggestion to you. I suggest you override the touchesBegan: method and check if the number of taps in the button is 2. If so, then start a timer that does what you need it to do, for as long as you need it to be done, and then on the touchesEnded: method, you'll go ahead and stop that timer, and this way you will have done whatever it is that you needed to do, for as long as you needed to do it OR as long as the user has held on to the button.

    I hope this helps, obviously I didn't write the whole code for you, you'll have to experiment and research that stuff, but if you have any questions, I'll be happy to lend a helping hand :)

提交回复
热议问题