私は、UICollectionView addGestureRecognizerを使用してタッチジェスチャ認識プログラムを追加し、タッチジェスチャを処理することをお勧めします(例えば、コレクションビューのタッチ位置を取得し、タッチしたアイテムのindexPathを取得する、次に自分自身でcollectionView.didSelectItemAtIndexPathを呼び出します)。スクロールについては、UISrollViewDelegateメソッドを使用して、スクロールが開始されるとコレクションビューのユーザー操作を無効にしてから、スクロールが停止した後、および/またはviewDidDisappearビューコントローラーの機能でユーザーの操作を再度有効にできます。このよう
:
パブリッククラスMyViewController:のUIViewController {
@IBOutlet弱いのvar collectionView:UICollectionView!
var collectionViewTap:UITapGestureRecognizer?viewDidLoad FUNC
オーバーライド公共viewDidDisappear FUNC(){
collectionViewTap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
self.view.addGestureRecognizer(collectionViewTap!)
}
オーバーライドパブリック(アニメーション:ブール値){
collectionView.userInteractionEnabled = true
}
FUNCのhandleTap(送信者: UITapGestureRecognizer){
let touchPoint = sender.locationOfTouch(0, inView: collectionView)
let indexPath = collectionView.indexPathForItemAtPoint(touchPoint)
if (indexPath != nil) {
collectionView(collectionView, didSelectItemAtIndexPath: indexPath!)
}
}
公共FUNC scrollViewWillBeginDragging(scrollView:UIScrollViewの){
collectionView.userInteractionEnabled = false
}
公共FUNCのscrollViewDidEndDecelerating(scrollView:UIScrollViewの){
collectionView.userInteractionEnabled = true
}
}