【问题标题】:swipe to delete in a UITableView which is embeded in a UIScrollView滑动删除嵌入在 UIScrollView 中的 UITableView
【发布时间】:2012-07-07 23:05:52
【问题描述】:

我遇到了和UIScrollview enable delete row by swipe一样的问题
它是一个 tableView 和另一个视图作为 scrollView 的子视图工作,在我将 scrollView 的 scrollEnable 属性设置为 NO 之前,我无法启用“滑动删除” ,但它带来了另一个问题:我无法在 tableView 和另一个视图之间滑动
除了设置 scrollEnable 属性以启用“滑动删除”之外,还有其他方法吗?
如果不是,我应该什么时候设置self.scrollEnable = NO,什么时候应该设置self.scrollEnable = YES 让“滑动删除”和“在视图之间滑动”都可以正常工作?

谢谢

【问题讨论】:

    标签: ios uitableview ios5 ios4 uiscrollview


    【解决方案1】:

    如果,我没记错的话,滚动视图消耗了触摸,并且表格的编辑没有发生,因为表格没有得到触摸。这可以通过继承 UIScrollView 来解决,以便将触摸也发送给下一个响应者。所以这只是覆盖 touchesBegan、move 和 end 的问题。今天晚些时候将使用我现在在路上所需的代码更新答案。干杯!

    编辑:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if(!self.dragging)
        {
            [self.nextResponder touchesMoved:touches withEvent:event];
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesEnded:touches withEvent:event];
    }
    

    只需创建一个继承自UIScrollView 的类,并在实现中删除此代码。这将使 scrollView 不会吞下触摸,而是将它们传递下去。显然,在创建你的滚动视图时使用你刚刚创建的类而不是UIScrollView。 抱歉耽搁了。希望这会有所帮助。

    干杯!

    【讨论】:

      【解决方案2】:

      我已经成功使用

      - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
      

      在包含 tableview 的 UIScrollView 子类中,使驻留在 tableview 中的 UISwipeGestureRecognizer 能够触发而不是被“主”滚动视图的手势识别器吞噬。

      【讨论】:

        【解决方案3】:

        @THOR 的回答没问题,但是如果您的 UITableViewUIScrollView 中,那么您可能还有另一个 UIView 在那里。当您向上滚动tableview 时,您不小心滑到了“其他视图”。

        这将防止滑动,并允许您滑动删除。

        -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        if (gestureRecognizer.state != 0) {
                return YES;
        } else {
            return NO;
        }
        

        }

        【讨论】:

          【解决方案4】:

          您需要使用UIScrollView 的自定义子类。它应该适用于水平滚动视图中的表格视图:

          @interface MyCoolScrollView : UIScrollView
          
          @end
          
          @implementation MyCoolScrollView
          
          // Allows inner UITableView swipe-to-delete gesture
          - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
          {
              return [otherGestureRecognizer.view.superview isKindOfClass:[UITableView class]];
          }
          
          @end
          

          【讨论】:

          • @Peymankh 您还需要在其中包含本杰明哈洛克的答案才能在 iOS 11 中使用。
          【解决方案5】:

          我知道这个线程很旧,但这里是 swift 4 版本,为我在 iOS 11 中工作(你可以继承 UIScrollView):

          func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
              if (otherGestureRecognizer.view != nil && otherGestureRecognizer.view!.superview != nil) {
                  return otherGestureRecognizer.view!.superview!.isKind(of: UITableView.self)
              }
          
              return false
          }
          
          func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
              if (gestureRecognizer.state != .possible) {
                  return true
              }
          
              return false
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-12
            • 1970-01-01
            • 1970-01-01
            • 2012-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-01
            相关资源
            最近更新 更多