tags:

views:

216

answers:

1

hi Guys,

I created a UIButton programmatically and I want to create an action to handle a single tap and double tap for that button.

My question is how can I create this action, should I create separate action methods for this two events of just a single action?

Can you please provide a very basic code for this events?

Thanks a lot.

+4  A: 

My advice is don't because I don't think it's acceptable UI if you want to get it on the app store. But:

- buttonTapTimeout {
  self.doubletap = NO;
  // do your single tap action here
}

- buttonHandler {
  if(self.doubleTap == NO) {
    self.doubletap = YES;
    // I'm making this up, you will have to look up the function to set a timer 
    self.tapTimer = newTimer(/*delay*/ 0.5, /*action*/ buttonTapTimeout);
  } else {
    [self.tapTimer cancel];
    self.doubleTap = NO;

    // do your doubletap action here
}

Of course your single tap action can't happen until the acceptable delay for a second tap has expired.

Adam Eberbach
For the timer one could use -performSelector:withObject:afterDelay: and +cancelPreviousPerformRequestsWithTarget:selector:object: or similar methods. http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
Thomas Müller
That worked great for me, Thomas. Very convenient. (For anyone else, note that `+cancelPreviousPerformRequestsWithTarget:selector:object` is a NSObject *class* method.)
zekel