代码之家  ›  专栏  ›  技术社区  ›  Mc.Lover

如何识别保持按钮iPhone SDK

  •  1
  • Mc.Lover  · 技术社区  · 15 年前

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

    2 回复  |  直到 13 年前
        1
  •  6
  •   Andiih    15 年前

    触地内事件触发,启动计时器。 触发TouchupInside事件,取消计时器。 如果用户按住按钮,则让计时器调用要执行的方法:计时器延迟将是识别保持所需的时间量。

        2
  •  5
  •   Community CDub    8 年前

    您也可以使用 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
    }
    

    注意 UILongPressGestureRecognizer 是一个 continuous event recognizer . 当用户仍按住按钮时, 我的按钮长按: 将被多次调用。 如果您只想处理第一个调用,可以在 我的按钮长按: :

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