Conflicting UITapGestureRecognizer in UIView and for UITableView

后端 未结 2 1839
粉色の甜心
粉色の甜心 2021-02-01 18:20

I have a UIView in which I added a UITapGestureRecognizer. Inside that view I also have a subview in which is basically some kind of a UITableVie

相关标签:
2条回答
  • 2021-02-01 18:47

    If you want both your UITableView and your UITapGestureRecognizer to receive touch events, then yes the cancelsTouchesInView = NO will work. If you want the tap gesture recognizer not to receive the touch events meant for the table view it is slightly less easy but very do-able.

    Basically when you are creating your gesture recognizer you set self as its delegate. Then you implement the gestureRecognizer:shouldReceiveTouch: delegate method. A basic implementation might look like this.

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        UITableView *tableView = self.tableView;
        CGPoint touchPoint = [touch locationInView:tableView];
        return ![tableView hitTest:touchPoint withEvent:nil];
    }
    

    Essentially this method (as implemented) asks the tableView if this touch's location falls within the tableView's jurisdiction, and if it does, it will block the gesture recognizer from receiving the touch...allowing the tableView to receive the touch.

    0 讨论(0)
  • 2021-02-01 18:56

    Set cancelsTouchesInView of your recognizer to NO. Otherwise, it "consumes" the touch for itself, and does not pass it on to the table view. That's why the selection event never happens.

    0 讨论(0)
提交回复
热议问题