如何识别iOS中的单个单击和双击视图?
Posted
技术标签:
【中文标题】如何识别iOS中的单个单击和双击视图?【英文标题】:How to identify individual single click and double click view in iOS? 【发布时间】:2015-09-08 11:02:28 【问题描述】:在我的应用程序中,想要识别单击并双击查看。 我的实际问题是双击视图时也发生了单击。单击即可完美运行..所以 如何识别单个单击和双击视图?
我的代码是:
- (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];
【问题讨论】:
我认为手势是不可能的,如果你想知道使用触摸结束方法 您是否将这些添加到完全相同的视图中?在您的代码中,您要添加到 2 个不同的视图( image_scroll 和 self.view )... 我在主视图中添加了 2 个不同的视图图像滚动和子视图 您想通过单击与双击来实现什么?使用长按手势双击会更容易吗?这样你就可以保持单击。 在我的应用程序中有画廊视图。当单击发生时,页眉和页脚将像我们的手机画廊一样隐藏。当双击该时间图像放大 - 所以我想要解决方案为此 【参考方案1】:在您的应用程序中使用以下代码
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");
上面的代码完美运行。我尝试了上面的编码。它单独运行。检查它。
【讨论】:
我也试过这个..但是当双击那个时候出现同样的问题 欢迎我的朋友斯黛拉:)【参考方案2】:为两个手势添加相同的目标,
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:");
【讨论】:
不,我无法更改 [self.view addGestureRecognizer:singleTap]; 你可以改变你的看法,伙计,这是一个例子。 在发帖之前我已经测试过了。如果你的点击速度很慢,那么尝试增加目标延迟...在这段代码中是 0.5 秒。 没有仍然无法正常工作..请给我另一个解决方案 让我们continue this discussion in chat。以上是关于如何识别iOS中的单个单击和双击视图?的主要内容,如果未能解决你的问题,请参考以下文章