-2

I'm confused about LongPressGestureRecognizer.I placed one of these on a scroll view,but it works two times.When I lift my finger,the methods added on it called again.I wonder it only called first time.What should I do?Any help will be appreciated, thanks.

KolinChen
  • 13
  • 6

2 Answers2

4

first look what apple docs have to say about it:-

"Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted."

 -  (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.

         }

      }
Anurag Bhakuni
  • 2,379
  • 26
  • 32
1

UILongPressGestureRecognizer is not as same as UITapGestureRecognizer. It contains some states.

- (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;
    }
}

For above codes, you will get 2 logs:

2015-08-28 12:22:39.084 aaaaa[50704:2339282] UILongPressGestureRecognizer: began
2015-08-28 12:22:40.687 aaaaa[50704:2339282] UILongPressGestureRecognizer: ended
Harrison Xi
  • 766
  • 1
  • 5
  • 23