Stacking UITableViews does not pass touch events b

2019-04-08 02:29发布

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.

2条回答
SAY GOODBYE
2楼-- · 2019-04-08 02:58

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.

查看更多
太酷不给撩
3楼-- · 2019-04-08 03:02

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

查看更多
登录 后发表回答