在我的应用程序中,想要识别单击并双击View。 我的实际问题是双击视图时单击也发生了。单击即可完美运行。所以 如何识别单击并双击视图?
我的代码是:
- (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];
}
}
答案 0 :(得分:3)
在您的应用程序中使用以下代码
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");
}
上面的代码很完美。我尝试了上面的编码。它可以单独工作。检查它。
答案 1 :(得分:0)
为两个手势添加相同的目标
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];
延迟第一个点击处理程序
- (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:");
}