UILongPressGesture检测两次

时间:2012-11-02 09:34:16

标签: objective-c uigesturerecognizer

在我的长按手势中,我有一个问题,

*mypressrec = [[UILongPressGestureRecognizer alloc]
                                         initWithTarget:self 
                                         action:@selector(pressdetected:)];
    mypressrec.minimumPressDuration = 3;
    [self addGestureRecognizer:mypressrec];
    [mypressrec release];

我的职能:

    -(void)pressdetected:(UILongPressGestureRecognizer*)recognizer{
     //My code goes here
  a=90;
   NSLog(@"value of my A",a);
}

这里当我按下超过3秒时,我A的值正在打印 TWICE 。为什么会这样?

1 个答案:

答案 0 :(得分:4)

要检查UILongPressGestureRecognizer的状态,只需在选择器方法上添加if语句:

- (void)pressdetected:(UILongPressGestureRecognizer*)sender { 
if (sender.state == UIGestureRecognizerStateEnded) {
    NSLog(@"Long press Ended");
}
else {
    NSLog(@"Long press detected.");
}
}