我有一个UIView自定义类,并且由此类创建的对象具有一个较长的手势识别器,为此

UILongPressGestureRecognizer *tapAndHold =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(doStuff:)];
    [tapAndHold setDelegate:self];
    [tapAndHold setCancelsTouchesInView:NO];
    [tapAndHold setDelaysTouchesEnded:NO];
    [self addGestureRecognizer:tapAndHold];
    [tapAndHold release];


问题在于,对于对象的每次长按,doStuff方法将依次运行两次。

我错过了什么吗?谢谢

最佳答案

手势识别器可能会触发多次,因为有多个相关事件需要通知您的代表。例如,一旦怀疑正在进行长按,它就会触发,而当实际长按结束时,它可能会再次触发。

手势识别器将向自身发送一个引用,您可以使用该引用来查询导致调用选择器的事件的性质。基本上,您将检查手势识别器的state属性,并根据其值确定是否应执行方法代码。

更新:您的doStuff可能如下所示:

-(void)doStuff: (UIGestureRecognizer *)gestureRecognizer {

   if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
      //do the actual stuff
   }
}

关于iphone - iPhone-长按两次相同的方法发射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3769933/

10-09 01:25