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

后端 未结 3 435
耶瑟儿~
耶瑟儿~ 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:34

    Brad Larson's answer looks pretty good but here's another one that might give you a bit more flexibility/control of what you want or might want to do.

    You subclass UIButton, you override the touchesBegan and touchesEnded methods so that when the user starts a touch you call

    [self performSelector:@selector(detecetedLongTap) withObject:nil afterDelay:1.0];
    

    and in the touchesEnded you call:

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(detecetedLongTap) object:nil];
    

    to cancel the event if the finger was lifted too soon.

    You can get full code for this in this blog post:

    http://www.isignmeout.com/adding-long-tap-functionality-uibutton/

提交回复
热议问题