【问题标题】:Stacking UITableViews does not pass touch events beneath its view堆叠 UITableViews 不会在其视图下方传递触摸事件
【发布时间】:2011-12-28 23:43:05
【问题描述】:

我有 3 个 UIViews 堆叠在另一个之上

UITableview
平面视图
根视图

TableView 位于顶部,rootView 位于底部。 (rootView 不可见,因为 TableView 在它上面)

我在rootView中实现了以下代码

/*code in rootView*/



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}  

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {} 

期望这些函数会在最顶层的视图即 TableView 被触摸或移动时被调用,但相反没有调用任何函数。

我还尝试将以下代码放入 TableView 中,以便调用 rootView 方法

 /*code in TableView so that the rootView methods are called(TableView is the subview of rootView)*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
 {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
 }

正如预期的那样,但问题是 TableView 代表喜欢

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    

没有被调用。

有什么方法可以确保TableView类中实现的TableView委托(didSelectRow:)和rootView中的touchesBegan:,touchesMoved..函数也被相应调用?

即当我单击 TableCell 时,会调用 (didSelectRow:atIndex) 函数 in--> TableView 和 (touchesBegan and touchesEnd) 方法 in-->rootView。

【问题讨论】:

    标签: uitableview uiview uievent


    【解决方案1】:

    UITableView 的子类中,您应该有这样的触摸方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesBegan:touches withEvent:event];
        [super touchesBegan:touches withEvent:event];
    }
    

    这里的区别在于您将触摸传递给下一个响应者而不是超级视图,并且您在将触摸传递给超级视图之前执行此操作

    然后在planeView 中你需要像这样传递触摸:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.superview touchesBegan:touches withEvent:event];
    }
    

    请记住,这可能仍无法完全按照您的预期工作。 UITableView 对引擎盖下的响应者链进行了很多修改,以使 UITableView(实际上是子视图的复杂集合)看起来就像按钮或标签之类的另一个视图。

    【讨论】:

    • 分辨率很高。感谢您的帮助
    • 但仍然在某个时间点 touchesEnded: 未被调用。我在 touchesEnded: 消息处对视图位置进行了一些调整,但由于消息未传递,因此视图调整无法正常工作我期待。
    • @prajul:是的,我目前正在努力解决类似的问题。表格视图是一个非常复杂的野兽。根据您使用底层触摸的目的,我尝试的另一件事是将完全透明的 UIView 放置在我的表格视图之上。如果您像我上面的代码一样通过触摸,表格视图仍然无法正常工作(您可以单击它但不能以任何方式滚动它)。但是,如果您改为通过 hitTest:withEvent:,表格视图将正常工作,您可以使用覆盖 uiview 中的初始 hitTest 来执行单击按钮或其他操作等操作。
    • 我知道这个问题已经很老了,但我很好奇是否找到了解决某些未通过的触摸的解决方案。我在 tableview 单元格中有一个 tableview,并按照建议覆盖了 touchesBegan、touchesMoved、touchesEnded 和 touchesCancelled。它在大多数情况下都有效,但在某些情况下它的行为有点奇怪。
    【解决方案2】:

    这些都不适合我

    解决方法很简单:

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        return view == self ? nil : view
    }
    

    参考这篇文章:https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多