UICollectionViewCell with UIScrollView cancels didSelectItemAtIndexPath

后端 未结 4 1115
野的像风
野的像风 2020-12-14 18:47

I have a UICollectionView that horizontally scrolls to show one UICollectionViewCell at a time. Each UICollectionViewCell the has a ve

4条回答
  •  忘掉有多难
    2020-12-14 19:22

    Add tap gesture to your super view

    override func viewDidLoad() {
            super.viewDidLoad()
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView(gesture:)))
            view.addGestureRecognizer(tapGesture)
        }
    
    @objc func didTapView(gesture: UITapGestureRecognizer) {
        view.endEditing(true)
        let touchLocation:CGPoint = gesture.location(ofTouch: 0, in: self.collectionView)
        let indexPath = self.collectionView.indexPathForItem(at: touchLocation)
        if indexPath != nil {
            let cell = self.collectionView.cellForItem(at: indexPath!)
            if (cell?.isSelected)! {
                //PREFORM DESELECT
            } else {
                //PREFORM SELECT HERE
            }
        }
    } 
    

提交回复
热议问题