堆叠 UITableViews 不会在其视图下方传递触摸事件 [英] Stacking UITableViews does not pass touch events beneath its view

查看:23
本文介绍了堆叠 UITableViews 不会在其视图下方传递触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个 UIViews 一个堆叠在另一个顶部

I am having 3 UIViews stacked one on top of another

UITableview
平面视图
根视图

UITableview
planeView
rootView

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

TableView is at the top and rootView at the bottom. (rootView is not visible as TableView is on top of it)

我在rootView中实现了以下代码

I have implemented the following code in 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被触摸或移动时会调用这些函数,但相反没有调用任何函数.

expecting that these functions will be called when the top most view ie TableView is touched or moved,but on the contrary none of the functions were called.

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

I also tried putting the following code in TableView so that the rootView methods are called

 /*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 像

As expected it did so but the problem is that the TableView delegates like

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

没有被调用.

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

Is there any way by which it can be ensured that the TableView delegates implemented in TableView class(didSelectRow:) and the touchesBegan:,touchesMoved.. functions in rootView are also called accordingly?

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

ie When i click on a TableCell both (didSelectRow:atIndex) function in--> TableView and (touchesBegan and touchesEnd) method in-->rootView are called.

推荐答案

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

In your subclass of UITableView you should have touch methods like this:

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

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

The difference here is that you're passing the touch to the next responder instead of the superview, and you're doing this before passing the touch to super.

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

Then in planeView you need to pass touches like this:

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

请记住,这仍然可能无法完全按照您的预期工作.UITableView 在幕后对响应链进行了大量修改,以使其看起来好像 UITableView(实际上是一个复杂的子视图集合)只是另一个视图,如按钮或标签.

Keep in mind that this still may not work exactly as you expect. UITableView does a lot of mangling of the responder chain under the hood, in order to make it seem as if a UITableView (which is actually a complex collection of subviews) is just another view like a button or a label.

这篇关于堆叠 UITableViews 不会在其视图下方传递触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆