双击UIButton

dom*_*lao 0 iphone

我以编程方式创建了一个UIButton,我想创建一个动作来处理单击并双击该按钮.

我的问题是如何创建此操作,我应该为这两个事件创建单独的操作方法吗?

你能为这次活动提供一个非常基本的代码吗?

非常感谢.

Ada*_*ach 7

我的建议是不要因为我不认为这是可以接受的UI,如果你想在应用商店获得它.但:

- 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
}
Run Code Online (Sandbox Code Playgroud)

当然,在第二次点击的可接受延迟到期之前,您的单击动作不会发生.

  • 对于计时器,可以使用-performSelector:withObject:afterDelay:和+ cancelPreviousPerformRequestsWithTarget:selector:object:或类似的方法.http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html (2认同)