【问题标题】:How to recognise gestures on pop-up view without interrupting gesture recognition on the view underneath?如何在弹出视图上识别手势而不中断下方视图上的手势识别?
【发布时间】:2016-04-05 09:59:20
【问题描述】:

我有一个带有自定义手势识别器的自定义半透明视图,它可以识别单指手势。它会在全屏视图上弹出。用户使用捏合和旋转手势与全屏视图进行交互。

所以我想要实现的是防止弹出视图干扰用户继续捏合和旋转全屏视图的能力,即使在弹出视图的范围内也是如此。弹出视图只需要响应我自定义的单指手势识别器即可。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (event.allTouches.count > 1) {
        return NO;
    }

    ...
}

我认为上面的代码可能已经破解了它,但遗憾的是当调用pointInside 时没有填充触摸。请问还有什么建议吗?

【问题讨论】:

    标签: ios uiview uigesturerecognizer


    【解决方案1】:

    我认为您将需要UIGestureRecognizerDelegate 协议。特别是那些方法:

    - gestureRecognizer:shouldReceiveTouch:
    
    - gestureRecognizer:shouldRequireFailureOfGestureRecognizer:
    - gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:
    

    【讨论】:

    • 我试过总是在自定义手势识别器的shouldReceiveTouch 上返回NO。这当然会阻止识别器激活,但弹出视图中的触摸仍然无法到达下方全屏视图的手势识别器。
    【解决方案2】:

    您可以使用以下方法

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
    

    我使用了 touchesBegan,我没有接触,并且根据条件你可以做任何你想做的事情。

    下面是示例代码

     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //Varible used for counting touches
            int nFingers += [touches count];
            //finger points
            for (UITouch *touch in touches) {
                if (nFingers < 1){
                    CGPoint location = [touch locationInView:self];
                }
            }
        }
    

    【讨论】:

      【解决方案3】:

      经过两天的工作,我找到了一个非常简单的答案。我希望它可以帮助某人。在弹出视图的初始化中,只需将手势识别器添加到超级视图或任何全屏视图,而不是弹出窗口自己的视图。目标仍然可以是弹出视图:

          self.recognizer = [[MyGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
          [fullscreenView addGestureRecognizer:self.recognizer];
      

      然后禁用用户交互:

          self.userInteractionEnabled = NO;
      

      这不会影响弹出视图上的手势,并且与弹出窗口下方的视图关联的手势现在也可以正常工作。如果它们相互竞争,那么 lorenzoliveto 的答案中的覆盖可以对其进行排序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        相关资源
        最近更新 更多