长按手势识别器被解雇了两次

时间:2015-08-28 03:51:50

标签: ios objective-c long-press

我对LongPressGestureRecognizer感到困惑。我将其中一个置于滚动视图中,但它可以工作两次。当我抬起手指时,添加的方法再次调用。我不知道它只是第一次调用。我该怎么办?感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:3)

首先看看苹果医生对此有何看法: -

"长按手势是连续的。当指定时间段(minimumPressDuration)按下允许的手指数(numberOfTouchesRequired)并且触摸不超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。每当手指移动时,手势识别器转换到更改状态,并且当任何手指被抬起时,手势识别器结束(UIGestureRecognizerStateEnded)。"

 -  (void)LongPress:(UILongPressGestureRecognizer*)sender { 

        if (sender.state == UIGestureRecognizerStateBegan){
           NSLog(@"UIGestureRecognizerStateBegan.");
       //in your case add your functionality over here
         }
        else if (sender.state == UIGestureRecognizerStateEnded) {
          NSLog(@"UIGestureRecognizerStateEnded");
        //if you want to add some more functionality when gesture got ended.

         }

      }

答案 1 :(得分:1)

UILongPressGestureRecognizer与UITapGestureRecognizer不同。它包含一些州。

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
    [self.view addSubview:scrollView];

    UILongPressGestureRecognizer *lpGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpHandler:)];
    [scrollView addGestureRecognizer:lpGes];
}

- (void)lpHandler:(UILongPressGestureRecognizer *)lpGes
{
    switch (lpGes.state) {
        case UIGestureRecognizerStateBegan:
            NSLog(@"UILongPressGestureRecognizer: began");
            break;

        case UIGestureRecognizerStateEnded:
            NSLog(@"UILongPressGestureRecognizer: ended");
            break;

        default:
            break;
    }
}

对于上述代码,您将获得2个日志:

2015-08-28 12:22:39.084 aaaaa[50704:2339282] UILongPressGestureRecognizer: began
2015-08-28 12:22:40.687 aaaaa[50704:2339282] UILongPressGestureRecognizer: ended