【问题标题】:Action Trigger when I hold UIButton for 2 second in iPhone当我在 iPhone 中按住 UIButton 2 秒时的动作触发
【发布时间】:2011-06-16 11:06:57
【问题描述】:

我的应用程序中有一个UIButton 和一个在我触碰UIButton 时触发的操作。

是否可以在 iPhone 上检测到 UIButton 上的触摸并按住?我希望当用户按住按钮 2 秒或更长时间时触发我的操作。

有什么想法吗?

【问题讨论】:

    标签: ios4 ios-simulator iphone


    【解决方案1】:

    另一方面,您可以使用this NBTouchAndHoldButton。这正是你想要的,而且很容易实现:

    TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
    [pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2];
    

    祝你好运!

    【讨论】:

      【解决方案2】:

      UILongPressGestureRecognizer 是您所需要的。例如,

      UILongPressGestureRecognizer *longPress_gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doAction:)];
      [longPress_gr setMinimumPressDuration:2]; // triggers the action after 2 seconds of press
      [yourButton addGestureRecognizer:longPress_gr];
      

      要让您的操作只触发一次(即,当 2 秒持续时间结束时),请确保您的 doAction: 方法看起来像这样,

      - (void)doAction:(UILongPressGestureRecognizer *)recognizer {
      
          if (recognizer.state == UIGestureRecognizerStateBegan) {
      
              // Your code here
          }
      }
      

      【讨论】:

      • 我试过你的代码,它工作正常。但是我的动作触发两次有一个问题。 2秒后第一个动作触发,当我结束触摸时触发。
      • @Arman:哦..我已经编辑了我的答案。我想这应该可行。看看吧。
      • 谢谢西蒙。它对我真的很有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多