6

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

UITableview
planeView
rootView

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 委托和 rootView 中的 touchesBegan:,touchesMoved.. 函数也被相应地调用?

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

4

2 回答 2

6

在你的子类中,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在引擎盖下对响应者链进行了很多修改,以使其看起来好像 a UITableView(实际上是子视图的复杂集合)只是另一个视图,如按钮或标签。

于 2011-11-14T20:43:42.627 回答
0

这些都不适合我

解决它的方法很简单:

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

于 2018-05-18T15:00:36.300 回答