Tuesday, May 28, 2013

Identify single tap and double tap in ios

I had faced this kind of issue with gestures which is I have used custom gesture for single tap and double tap gesture is inbuilt but double tap gesture is not visible to others you cannot access this gesture when creating the single tap gesture.
The issue is when i double tap on the page this app get it as this is two single tap So it is running single tap task.

I used following scenario for solving this issue.

1. Create single tap gesture like this

tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toggleBars)];tapGestureRecognizer.numberOfTouchesRequired = 1;[self.view addGestureRecognizer:tapGestureRecognizer];tapGestureRecognizer.delegate = self;

2.create target function like this

- (void)toggleBars
{
   [self performSelector:@selector(performToggleBars) 
              withObject:nil 
              afterDelay:0.4];
}

- (void)performToggleBars
{
   //task you need to process.
}

3. There is UIGestureRecognizerDelegate delegate method you need to override like this

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
       shouldReceiveTouch:(UITouch *)touch
{
  if (touch.tapCount != 1)
  {
    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                            selector:@selector(performToggleBars)
                                              object:nil];
  }

  return YES;
}

in this scenario cancel the single tap gesture event if it is multiple taps.

No comments:

Post a Comment