如何同时将UILongPressGestureRecognizer和UITapGestureRecognizer添加到同一控件?

时间:2015-03-31 10:56:45

标签: ios uigesturerecognizer gesture uitapgesturerecognizer simultaneous

类似于这个问题:

iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?

但我的问题更复杂。

我想实现您在iPad上的iOS 8中看到的相同行为。我的意思是Safari中的页面网格。

问题:一个视图应该响应长按和轻击手势识别器。以下事情应该有效:

1)关闭按钮接受点击

2)当点击开始时,所选视图应执行缩放动画

3)长按,所选视图变为可拖动

如果我不使用(requireGestureRecognizerToFail:),则点击手势无效。如果我使用这种方法,那么一切都会奏效,但长时间的新闻事件会发生很大的延迟。

如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

您需要使用requireGestureRecognizerToFail方法。

//Single tap
        UITapGestureRecognizer *tapDouble = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self
                                             action:@selector(handleTapGestureForSearch:)];
        tapDouble.numberOfTapsRequired = 1;
        tapDouble.delegate = self;
        [self addGestureRecognizer:tapDouble];

        //long press
        UILongPressGestureRecognizer *longPressGestureRecognizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressRecognizer:)];
        longPressGestureRecognizer.numberOfTouchesRequired=1;
        longPressGestureRecognizer.minimumPressDuration = 0.5f;
        [longPressGestureRecognizer requireGestureRecognizerToFail:tapDouble];
        longPressGestureRecognizer.delegate = self;
        [self addGestureRecognizer:longPressGestureRecognizer];

这意味着长按手势等待单击。

答案 1 :(得分:0)

您可以为长按手势添加时间。

 UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(ontappLongPressGesture:)];
 longPressGesture.minimumPressDuration=0.6;
 longPressGesture.delegate=self;
 [cell.view addGestureRecognizer:longPressGesture];
 UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc]    initWithTarget:self action:@selector(cellSelected:)];
//[gesture requireGestureRecognizerToFail:longPressGesture]; 
 gesture.delegate=self;
 [cell.view addGestureRecognizer:gesture];

 also you need to set this delegate to work both gesture together
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer  shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer  *)otherGestureRecognizer 
 {
 return YES;
 }