Search code examples
swiftuitableviewuipangesturerecognizerhittest

Pass UITableView default PanGesture to another UIView


I have an UITableView, vertically covered over a UIView and that View have UIPangestureRecognizer. Now if user try to scroll tableView and if finger point have no cell I want second views panGesture response.


Solution

  • Subclass UITableView and override hitTest method.

    -> if finger point have cell return self

    -> else return nil or any other view that should response

    Code:

    class MyTableView: UITableView {
        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            if indexPath(at: point) != nil {
                retrn self
            } else {
                return nil
            }
        }
    }