Monika Patel
Monika Patel

Reputation: 2375

How to identify individual single click and double click view in iOS?

In my application, want to identify single click and double click on View. My actual problem is when double click on view that time single click also occurred. And single click is working perfectly..So How to identify individual single click and double click on view ?

My code is :

- (void)viewDidLoad
{
   [super viewDidLoad];
   [self fullscreenGallery];
   [self video_image_Gallery];
}
-(void)video_image_Gallery
{
   UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
   doubleTap.numberOfTapsRequired = 2;
   doubleTap.delegate = self;
   [image_scroll addGestureRecognizer:doubleTap];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer 
{
   [self performSelector:@selector(fullscreenGallery) withObject:nil afterDelay:3];
   UIScrollView * imageScroll = (UIScrollView *)gestureRecognizer.view;
   self.header_view.hidden=NO;
   float newScale = [imageScroll zoomScale] + ZOOM_STEP;
   if (newScale > imageScroll.maximumZoomScale)
   {
      newScale = imageScroll.minimumZoomScale;
      CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
      [imageScroll zoomToRect:zoomRect animated:YES];
   }
   else
   {
      self.header_view.hidden=YES;
      newScale = imageScroll.maximumZoomScale;
      CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
      [imageScroll zoomToRect:zoomRect animated:YES];
   }
}

Upvotes: 3

Views: 763

Answers (2)

user3182143
user3182143

Reputation: 9589

Use below code in your application

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:    self action:@selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
singleTap.delaysTouchesEnded = YES;

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:   self action:@selector(handleDoubleTap)];
doubleTap.numberOfTapsRequired = 2;


[singleTap requireGestureRecognizerToFail:doubleTap];

[self.view addGestureRecognizer:singleTap];
[self.view addGestureRecognizer:doubleTap];


-(void)handleSingleTap
{
  NSLog(@"The single tap happened");
}

-(void)handleDoubleTap
{
  NSLog(@"The double tap happened");
}

Above code works perfectly.I tried above coding.it works individually.Check it.

Upvotes: 4

Rohit Kumar
Rohit Kumar

Reputation: 887

Add the same target for both gestures,

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

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[self.view addGestureRecognizer:singleTap];

delay the first tap handler

- (void) handleDoubleTap:(UITapGestureRecognizer*)sender {
    [self performSelector:@selector(handleSingleTap:) withObject:nil afterDelay:0.5];
    if(sender.numberOfTapsRequired == 2) {
        NSLog(@"Double tap:");
      [NSObject cancelPreviousPerformRequestsWithTarget:self];
    }
}

- (void) handleSingleTap:(UITapGestureRecognizer*)sender {
    NSLog(@"Single tap:");
}

Upvotes: 0

Related Questions