Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect long tap in UIScrollView

How would I detect a long tap (tap and hold) within a UIScrollView?

like image 554
Egil Jansson Avatar asked Dec 22 '22 04:12

Egil Jansson


2 Answers

In view's touchesBegan: you can call your "long tap" handle with some delay.

[touchHandler performSelector:@selector(longTap:) withObject:nil afterDelay:1.5];

Then in view's touchesEnded: you can cancel that call if not enough time has passed:

[NSObject cancelPreviousPerformRequestsWithTarget:touchHandler selector:@selector(longTap:) object:nil];
like image 93
Vladimir Avatar answered Jan 06 '23 17:01

Vladimir


//Add  gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):

    // Add long tap for the main tiles
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
    [tile addGestureRecognizer:longPressGesture];
    [longPressGesture release];

-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
    NSLog(@"gestureRecognizer= %@",gestureRecognizer);
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        NSLog(@"longTap began");

    }

}
like image 25
Gamma-Point Avatar answered Jan 06 '23 17:01

Gamma-Point