0

I have been trying to implement a button with two different touch events. Let's say when user taps the button (touches for short time), it triggers actionTapped and when user touches down the button for a long time it triggers actionTouched.

This link may give an idea but it makes action repeat itself over and over again.

4

4 回答 4

1

iOS SDK 包括两个非常适合您需求的手势识别器:

  • UITapGestureRecognizer
  • UILongPressGestureRecognizer

创建您的按钮而不附加任何操作。然后创建两个手势识别器,每种类型一个,每个都映射到您想要的操作。然后将手势识别器附加到按钮上。

于 2012-04-19T22:19:49.787 回答
1

您需要在 touchDown 事件上设置一个计时器,该计时器将执行您的 longPress 功能。在 touchUp 事件中,您可以取消计时器。真的比较简单。

于 2012-04-19T12:32:06.837 回答
0

我将执行以下操作:

  1. 在您的着陆方法中:存储当前时间(让我们调用它touchDownTime)。
  2. 在你的修饰方法中:计算time elapsed = current time - touchDownTime

    2.1 转换为秒

    2.2 如果经过时间 > 所需时间,请执行 action1(长按),否则执行 action2(短按)

于 2012-04-19T12:37:13.397 回答
0

好的,这是我的解决方案。scheduleTimerWithTimeInterval 是使其接触的最小间隔:

- (IBAction) micButtonTouchedDownAction {
    self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(micButtonAction:) userInfo:nil repeats:YES];
    self.micButtonReleased = FALSE;
}

- (IBAction) micButtonTouchedUpInsideAction {
    self.micButtonReleased = TRUE;
}

- (IBAction) micButtonTouchedUpOutsideAction {
    self.micButtonReleased = TRUE;
}

- (void) micButtonAction:(NSTimer *)timer {
    [self.micButtonTimer invalidate];
    self.micButtonTimer = nil;

    if(self.micButtonReleased) {
        NSLog(@"Tapped");
    }
    else {
        NSLog(@"Touched");
    }
}
于 2012-04-24T14:54:25.947 回答