【问题标题】:Passthrough touches of 2 UIScrollViews通过 2 ScrollView 的触摸
【发布时间】:2014-05-12 10:16:42
【问题描述】:

我有 UIScrollView 的 2 个后代

我有一个UITableView,它显示数据 我在UITableView上方添加了一个UICollectionView

view  
 | - UITableView
 | - UICollectionView

UITableView 只能垂直滚动,UICollectionView 只能水平滚动。我只能在collectionview 不重叠的地方滚动我的tableview(这是不符合预期的行为),但我需要这样做,以便我可以滚动我的tableview,即使我在我的collectionview 上垂直滑动。

由于其他原因,我不能简单地将collectionview 添加为tableview 的子视图(我知道,这会使这项工作发挥作用)

是否有任何其他可能让从collectionview 传递到tableview 的触摸?

【问题讨论】:

  • 嗨,你在吗?您需要放置包含 2 个子视图的主视图,第一个子视图是表格视图,第二个子视图是集合视图,那么只有它才能工作
  • 滚动视图直接添加为tableview的子视图然后它不会工作滚动会错过行为
  • 您想要实现的目标的屏幕截图会有所帮助。

标签: ios objective-c uitableview uicollectionview touches


【解决方案1】:

您可以尝试创建UICollectionView 的子类并将此代码添加到您的CustomCollectionView 的.m 文件中。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return nil;
    } else {
        return hitView;
    }
}

【讨论】:

    【解决方案2】:

    据我了解,您希望 UITableView 和 UICollectionView 拦截触摸?

    我认为您可以尝试将触摸事件从 UICollectionView 重新发送到 UITableView。 (手动调用touchesBegin、touchesMoved、touchesEnded等)

    也许覆盖 touchesBegan、touchesMoved、touchesEnded 方法适用于您的情况。

    您可以尝试使用您的子类(将属性设置为您的 UITableView 实例)覆盖 UICollectionView 并使用以下方式实现触摸处理方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
        if (CGRectContainsPoint(self.tableView.frame, [touch locationInView:self.tableView.superview]) {
           [self.tableView touchesBegan:touches withEvent:event];
        }
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
            [super touchesMoved:touches withEvent:event];
    
            [self.tableView touchesMoved:touches withEvent:event];
        }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
            [super touchesEnded:touches withEvent:event];
    
            [self.tableView touchesEnded:touches withEvent:event];
        }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
            [super touchesCancelled:touches withEvent:event];
    
            [self.tableView touchesCancelled:touches withEvent:event];
        }
    

    希望它会有所帮助,但我不能 100% 确定。

    我也找到了这篇文章,也许有用

    http://atastypixel.com/blog/a-trick-for-capturing-all-touch-input-for-the-duration-of-a-touch/

    【讨论】:

      【解决方案3】:

      您可以在collectionview上添加方向垂直的平移手势识别器。在垂直平移事件中,您可以更改表格视图的内容偏移量以滚动它。

      【讨论】:

      • 但是你没有开箱即用的反弹效果。
      猜你喜欢
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多