【问题标题】:How to Recognize a Hold Button {iPhone SDK}如何识别按住按钮 {iPhone SDK}
【发布时间】:2010-05-21 16:55:31
【问题描述】:

嗨,我有一个按钮,我想按住这个按钮写点东西,但我不知道如何识别按住按钮,你能帮我吗?谢谢你

【问题讨论】:

    标签: iphone sdk


    【解决方案1】:

    TouchDownInside 事件触发,启动一个 NStimer。 TouchUpInside 事件触发,取消定时器。 如果用户按住按钮,则让计时器调用您的方法来执行:计时器延迟将是识别按住所需的时间。

    【讨论】:

      【解决方案2】:

      您也可以使用UILongPressGestureRecognizer

      在您的初始化方法中(例如viewDidLoad),创建一个手势识别器并将其附加到您的按钮:

      UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] 
        initWithTarget:self 
        action:@selector(myButtonLongPressed:)];
      // you can control how many seconds before the gesture is recognized  
      gesture.minimumPressDuration = 2; 
      // attach the gesture to your button
      [myButton addGestureRecognizer:gesture];
      [gesture release];
      

      事件处理程序myButtonLongPressed: 应如下所示:

      - (void) myButtonLongPressed:(UILongPressGestureRecognizer *)gesture
      {
        // Button was long pressed, do something
      }
      

      请注意,UILongPressGestureRecognizercontinuous event recognizer。 当用户仍然按住按钮时,myButtonLongPressed: 将被多次调用。 如果你只是想处理第一个电话,你可以在myButtonLongPressed:查看状态:

      if (gesture.state == UIGestureRecognizerStateBegan) {
        // Button was long pressed, do something
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        相关资源
        最近更新 更多