Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double tapping problem

Im working on an app with zooming function. In this app I have this button. I want it to respond to tapping in several ways:

Single tap: Zoom in slightly.

Double tap: Zoom in to the max.

Ive tried several options to achieve this but none are what I want.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];

[zoomin addTarget:self action:@selector(zoominMax) forControlEvents:UIControlEventTouchDownRepeat];

Both work on single and double tap but when I press the button once to slightly zoom and seconds later I press it again it doesn't zoom in slightly, it zooms in to the max.

It is possible to fix this with a timer and location check so that when u tap and tap again u can be sure that the location is in a similar area and the taps happened within timer range.

But is this what I really need? Is there a simpler solution?

like image 588
Totumus Maximus Avatar asked Mar 23 '26 07:03

Totumus Maximus


2 Answers

the solution provided by omz is not good.

where as you can do this by simply adding these lines of code like posted here. Double-tap or two single-taps?

NOTE THE MAGICAL LINE : [tapRecg requireGestureRecognizerToFail:doubleTapRecg];

ABSTRACT:

UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(doubleTapped:)];
    doubleTapRecg.delegate = self;
    doubleTapRecg.numberOfTapsRequired = 2;
    doubleTapRecg.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:doubleTapRecg];


    UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self 
                                       action:@selector(tapped:)];
    tapRecg.delegate = self;
    tapRecg.numberOfTapsRequired = 1;
    tapRecg.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:tapRecg];
    [tapRecg requireGestureRecognizerToFail:doubleTapRecg];


    [doubleTapRecg release];
    [tapRecg release];
like image 141
yunas Avatar answered Mar 25 '26 19:03

yunas


You can do it with two gesture recognizers and a timer:

UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)] autorelease];
[myView addGestureRecognizer:tapGestureRecognizer];

UITapGestureRecognizer *doubleTapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)] autorelease];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[myView addGestureRecognizer:doubleTapGestureRecognizer];

You'll have to use a slight delay in your tap: action before zooming in slightly because the first tap could be followed by a second tap:

- (void)tap:(UITapGestureRecognizer *)recognizer
{
    [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.25];
}

- (void)singleTap
{
    //slightly zoom in...
}

- (void)doubleTap:(UITapGestureRecognizer *)recognizer
{
    //Cancel the timer for the single tap action:
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
    //zoom in to the max zoom level...
}
like image 39
omz Avatar answered Mar 25 '26 19:03

omz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!