Double tap Gesture on UICollectionViewCell?

China☆狼群 提交于 2019-12-11 12:17:40

问题


I want to double tap on the UICollectionViewCell to like the profile just like OkCupid App. I have applied Tap Gesture on Collection View but it does not work.

When I try to double tap the cell every time didSelectCollectionViewCell Method call.


回答1:


You have to add the double tap gesture recognizer to the collection view instead of the cell. In its action selector you could determine which cell was double tapped

override func viewDidLoad() {
     var doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didDoubleTapCollectionView:")
     doubleTapGesture.numberOfTapsRequired = 2  // add double tap
     self.collectionView.addGestureRecognizer(doubleTapGesture)
}

func didDoubleTapCollectionView(gesture: UITapGestureRecognizer) {
     var pointInCollectionView: CGPoint = gesture.locationInView(self.collectionView)
     var selectedIndexPath: NSIndexPath = self.collectionView(forItemAtPoint: pointInCollectionView)
     var selectedCell: UICollectionViewCell = self.collectionView.cellForItemAtIndexPath(selectedIndexPath)
     // Rest code
}



回答2:


The error which you got: Cannot call value of non-function type 'UICollectionView!' is because you have try to use wrong method.

Please try to use this one:

var selectedIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(pointInCollectionView)



回答3:


have you set the UITapGestureRecognizer property numberOfTapsRequired: to 2?




回答4:


apply the UITapGesture on UICollectionviewcell instead of UIcollectionView and handle the selection in custom UICollectionViewCell and set the property numberofTapsRequired equal to 2 .....



来源:https://stackoverflow.com/questions/34939214/double-tap-gesture-on-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!