【问题标题】:Disable all UIPanGestureRecognizers after `touchesBegan:withEvent:`在 `touchesBegan:withEvent:` 之后禁用所有 UIPanGestureRecognizer
【发布时间】:2013-07-11 23:03:31
【问题描述】:

有没有办法禁用触摸影响的所有UIPanGestureRecognizers?我希望能够将所有触摸事件隔离到我的一个子视图并让每个超级视图忽略所有触摸事件,但我只能在touchesBegan:withEvent: 之后确定这一点。

是否可以在触发touchesBegan:withEvent: 后阻止我的superview 的UIPanGestureRecognizers 与触摸进行交互?

【问题讨论】:

    标签: ios cocoa-touch uiresponder


    【解决方案1】:

    要在所有超级视图中禁用和重新启用平移,您应该执行以下操作:

    - (void)recursivelyEnable:(BOOL)enable panGesturesInSuperview:(UIView *)superview
    {
        for(UIPanGestureRecognizer *recognizer in superview.gestureRecognizers)
        {
            if([superview isKindOfClass:[UIScrollView class]])[(UIScrollView *)superview setScrollEnabled:enable];
            else [recognizer setEnabled:enable];
        }
        if(superview.superview)[self recursivelyEnable:enable panGesturesInSuperview:superview.superview];
    }
    

    并像这样使用它:

    //Disable panning
    [self recursivelyEnable:NO panGesturesInSuperview:self.superview];
    
    //Enable panning
    [self recursivelyEnable:YES panGesturesInSuperview:self.superview];
    

    出于某种原因,您不能乱用UIScrollViewUIGestureRecognizers 或其任何子类;这就是为什么我包含了平移的检查和替代禁用/启用。

    【讨论】:

      【解决方案2】:

      是的,使用此代码:

      yourGesture.enabled = NO;
      

      【讨论】:

      • 其实我弄错了。它确实有效。我愚蠢地调用了错误的方法。
      • 哦。我不知道为什么我不寻找“启用”属性。让我看看它在触摸后是否也有效。
      猜你喜欢
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多