Stacking UITableViews does not pass touch events beneath its view

ぃ、小莉子 提交于 2019-12-06 01:40:06

问题


I am having 3 UIViews stacked one on top of another

UITableview
planeView
rootView

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

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 {} 

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.

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];
 }

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

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

are not called.

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?

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


回答1:


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.

Then in planeView you need to pass touches like this:

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

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.




回答2:


None of this worked for me

What solved it was simply:

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

Ref this article: https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b



来源:https://stackoverflow.com/questions/8127721/stacking-uitableviews-does-not-pass-touch-events-beneath-its-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!