objective-c - ONLongPress手势检测两次

标签 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 的值将打印两次。为什么会发生这种情况?

最佳答案

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

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

关于objective-c - ONLongPress手势检测两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13192614/

相关文章:

iphone - 使用 UIPanGestureRecognizer 拖动后必须点击两次才能选择 UITableViewCell

iphone - 试图获得接触点,返回 NaN

ios - 如果内部识别器未收到触摸,则传递外部识别器

iphone - iOS 手动增加播放次数、上次播放日期等

iphone - 将垂直滚动手势传递给下面的 UITableView

ios - 如何识别 UISlider 的运动是从最大值到最小值,反之亦然?

ios - UICollectionView 在启用分页的情况下不会滚动完整的 320 点

ios - 由于UITapGestureRecognizer无法调用didSelectRowAtIndexPath

ios - cocos2d音频文件问题

iphone - 如何结合缩放和平移动画