简体   繁体   中英

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

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.

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:");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM