Conflicting UITapGestureRecognizer in UIView and for UITableView

后端 未结 2 1841
粉色の甜心
粉色の甜心 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.

提交回复
热议问题