容器视图的collectionVC被Parent的tapGesture识别器阻止

时间:2018-07-16 13:12:57

标签: ios swift uicollectionview uigesturerecognizer uitapgesturerecognizer

我具有UIViewController的扩展,如下所示,以在用户点击屏幕时关闭键盘。在此视图中,我有一个滚动条,其中包含文本字段和两个容器视图。容器视图包含collectionView。我希望我的整体viewController在点击时关闭,但我仍然希望触发collectionView的didSelectitemAtIndexPath。我该如何实现?

public extension UIViewController {
    public func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }

    public func dismissKeyboard() {
        view.endEditing(true)
    }
}

class RegisterVC: UIViewController {



    override func viewDidLoad() {
        super.viewDidLoad()
        logViewLoad()
        self.hideKeyboardWhenTappedAround()
        let controller = self.storyboard?.instantiateViewController(withIdentifier: "CollectionViewSearchVC") as! CollectionViewSearchVC
        controller.type = "SC"
        addChildViewController(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        controller.delegate = self
        containerView.addSubview(controller.view)

        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
            controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
            ])

        controller.didMove(toParentViewController: self)
        subContractorVC = controller

        let employeeController = self.storyboard?.instantiateViewController(withIdentifier: "CollectionViewSearchVC") as! CollectionViewSearchVC
        employeeController.type = "V"
        employeeController.delegate = self
        addChildViewController(employeeController)
        employeeController.view.translatesAutoresizingMaskIntoConstraints = false
        employeeContainerView.addSubview(employeeController.view)

        NSLayoutConstraint.activate([
            employeeController.view.leadingAnchor.constraint(equalTo: employeeContainerView.leadingAnchor),
            employeeController.view.trailingAnchor.constraint(equalTo: employeeContainerView.trailingAnchor),
            employeeController.view.topAnchor.constraint(equalTo: employeeContainerView.topAnchor),
            employeeController.view.bottomAnchor.constraint(equalTo: employeeContainerView.bottomAnchor)
            ])

        employeeController.didMove(toParentViewController: self)
    }

2 个答案:

答案 0 :(得分:1)

在您的ViewController中实现GestureRecogniser的委托方法,并检查要触摸的类(如果该类是UICollectionView),则忽略该触摸并让其执行默认行为,即didSelect否则允许点击。

class ParentViewController: UIViewController {
var tap: UITapGestureRecognizer?

override func viewDidLoad() {
    super.viewDidLoad()
    tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
    view.addGestureRecognizer(tap!)
}

public func hideKeyboardWhenTappedAround() {
    dismissKeyboard()
}

@objc public func dismissKeyboard() {
    view.endEditing(true)
}
}


class RegisterVC: ParentViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
  super.viewDidLoad()
  tap?.delegate = self
// YOUR EXISTING CODE HERE
}
// NEW CODE ADDED HERE
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    print("shouldReceive")
    print(gestureRecognizer.view as Any) // CHECK FOR WHICH CLASS YOU ARE GETTING HERE WHEN YOU CLICK ON COLLECTIONVIEW
    if (gestureRecognizer.view?.isKind(of: UICollectionView.self))! {
        return false
    }
    return true
}
}

尝试一下并分享结果。

答案 1 :(得分:0)

您需要在子VC的视图后面创建一个视图,并向其添加手势