堆叠UITableView不会在其视图下传递触摸事件

堆叠UITableView不会在其视图下传递触摸事件,uitableview,uiview,uievent,Uitableview,Uiview,Uievent,我有3个UIView,一个叠在另一个上面 UITableview 平面视图 根视图 TableView位于顶部,rootView位于底部。(rootView不可见,因为TableView位于其顶部) 我在rootView中实现了以下代码 /*code in rootView*/ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {} - (void)touchesMoved:(NSSet *)touches

我有3个UIView,一个叠在另一个上面

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类(didSelectRow:)中实现的TableView委托和TouchsBegind:、TouchsMoved。。根视图中的函数也会相应地被调用


ie当我单击一个TableCell时-->TableView中的(didSelectRow:atIndex)函数和-->rootView中的(touchesBegind和touchesEnd)方法都会被调用。

UITableView
的子类中,您应该有如下触摸方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.superview touchesBegan:touches withEvent:event];
}
这里的区别在于,您将触摸传递给下一个响应者,而不是superview,并且在将触摸传递给super之前执行此操作

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.superview touchesBegan:touches withEvent:event];
}
请记住,这仍然可能无法完全按照您的预期工作
UITableView
对引擎盖下的响应程序链进行了大量的破坏,以使它看起来好像一个
UITableView
(实际上是一个复杂的子视图集合)只是另一个视图,比如一个按钮或标签。

这些都不适合我 解决问题的办法很简单:

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

参考本文:

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