0
votes

I have a tableView with two different custom UITableViewCells. The first cell in the tableView is ALWAYS of type ResearchPostFeedTableViewCell, and the rest of type CommentTableViewCell. The first cell type contains a subView that when tapped I would like it to do something. My problem is, I have added a UITapGestureRecognizer to UIViewController.view that hides the keyboard. As a result, tapping anywhere on the screen will only hide the keyboard. How do i disable this tap gesture specifically when the user taps on the subView of the very first cell? I have tried the following but I suspect I have not quite got the code correct for locating the touch on the subView of the first cell in order to disable the UIViewController.view tapGesture.

class CommentMainViewController: UIViewController, UITextViewDelegate, UIGestureRecognizerDelegate{

    var myTableView: CommentOnPostTableView! // Custom UITableView
    var screenTapGesture: UITapGestureRecognizer!

    override func viewDidLoad() {
        super.viewDidLoad()

    ...
    screenTapGesture = UITapGestureRecognizer.init(target: self, action: #selector(hideKeyBaord))
    screenTapGesture.delegate = self
    self.view.addGestureRecognizer(screenTapGesture)
    ...


}

    func hideKeyBaord(){
        // Hides the keyboard
    }


    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {

    if gestureRecognizer == screenTapGesture{
        let researchCell = myTableView.cellForRow(at: [0,0]) // First cell only
        if let imageCarousel = researchCell?.imageView{
            let location = touch.location(in: imageCarousel)
            let isTouchImage = imageCarousel.frame.contains(location)
            if isTouchImage{
                print("IMAGE TOUCHED!")
                // Disable screenTapGesture
                return false
            }else{
                return true
            }
        }
            return true
        }else{
            return false
        }
    }
}
1

1 Answers

2
votes

I think your problem is that the tap gesture which hides the keyboard cancels other touches.

Simply set the cancelsTouchesInView property of your tap gesture to false like this:

screenTapGesture.cancelsTouchesInView = false