0

我有一个增加数量的按钮,当我点击它时,我会拨打服务电话。问题是,如果我多次点击它,我只想在完成点击后拨打服务电话。我尝试使用 UITapGestureRecognizer 但我不知道如何计算点击次数。

任何帮助,将不胜感激。

这是示例代码

- (void)quantityChanged:(UITapGestureRecognizer *)tapRecognizer {
    static NSUInteger numberOfTaps;
    if (tapRecognizer.state == UIGestureRecognizerStateBegan) {
       numberOfTaps = 0;
    }
    if (tapRecognizer.state == UIGestureRecognizerStateEnded)
        NSLog(@"%d",numberOfTaps);
    if (tapRecognizer.state == UIGestureRecognizerStateChanged)
        numberOfTaps++;

}

4

4 回答 4

4

四种可能的解决方案

我认为你的方法很好

- (void)quantityChanged:(UITapGestureRecognizer *)tapRecognizer {

    static NSUInteger numberOfTaps;


   if (tapRecognizer.state == UIGestureRecognizerStateEnded) {

        numberOfTaps ++;

        NSLog(@"web service call with %d",numberOfTaps);
    }

}

查看日志

web service call with 1
web service call with 2
web service call with 3
web service call with 4

或者

触发后删除 quantityChanged UITapGestureRecognizer

我的意思是,从发件人对象中删除 UITapGestureRecognizer(在您的第一个 Web 服务之后的 quantityChanged 方法内)

然后在您收到第一个 Web 服务响应后,再次将 UITapGestureRecognizer 添加到发件人。

或者

使用 UILongPressGestureRecognizer 。这是一个漫长的事件过程。请检查事件状态,无论是开始、完成等,然后编写您的代码。

- (IBAction)longPressDetected:(UIGestureRecognizer *)gestureRecognizer {        

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){

        NSLog(@"Long press began");

    } else if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized ) {

            NSLog(@"Long press UIGestureRecognizerStateRecognized");
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

        NSLog(@"Long press Ended");
    }
    else {

        NSLog(@"Long press detected.");
    }       
}

或者

为 UITapGestureRecognizer设置numberOfTapsRequired

UITapGestureRecognizer *doubleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginComicTransitions:)] autorelease];    
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
doubleTapRecognizer.delegate = self;
于 2012-08-15T06:56:39.630 回答
1

即使我怀疑这会导致实用的用户界面......如果您知道用户可以“放入”的最大计数,您可以使用几个相互依赖的轻击手势侦听器- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer文档在这里)。

代码可能如下所示:

-(void)addGestureRecognizers
{
  int maxCount = 10;
  UITapGestureRecognizer *other = nil;
  for(int i = maxCount; i < 0; i--)
  {
    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
    tapper.numberOfTapsRequired = i;
    if(other)
    {
      [tapper requireGestureRecognizerToFail:other]; 
    }
    [self.view addGestureRecognizer:tapper];
    other = tapper;
  }
}

-(void)tapped:(UITapGestureRecognizer *)tapper
{
  [self callWebservice:tapper.numberOfTapsRequired];
}

我没有对其进行测试,也从未使用过它,但这就是我从文档中了解到的。

于 2012-08-15T07:39:58.110 回答
0

好的。所以我找到了解决我的问题的方法。我将以 0.5 秒的延迟执行我的 Web 服务调用,每次按下按钮时,我都会取消所有等待执行的选择器并再次调用 performSelector: withDelay。

于 2012-08-15T07:34:55.470 回答
0

没有 UITapGestureOrganizer 就更容易了。

button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)

接着:

func multipleTap(_ sender: UIButton, event: UIEvent) {
    let touch: UITouch = event.allTouches!.first!
    if touch.tapCount == 3 {
        print("triple tap")
    }
}
于 2018-11-21T09:36:13.177 回答