在 collectionview 的 uiimage 上点击手势

tap gesture on uiimage in collectionview

在我 UICollectionView 的每个单元格中,我有多个要与之交互的对象。 因此,我没有使用 didSelect 委托方法,而是真的想在单元格的每个对象上添加一个点击手势。

为简单起见,我删除了示例中的所有其他对象:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

  let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! PlacesCollectionViewCell

  let tap = UITapGestureRecognizer(target: self, action: "gotToSelectedPlace:")
  tap.numberOfTapsRequired = 1

  cell.imageView.userInteractionEnabled = true
  cell.imageView.addGestureRecognizer(tap)
  cell.imageView.file = places[indexPath.row].picture
  cell.imageView.loadInBackground()

  return cell

}

在viewDidLoad中,我使用了笔尖:

collectionView.registerNib(UINib(nibName: "PlacesCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell")

UICollectionView 设置:

在这个例子中,我无法处理点击手势。什么都没发生。 我错过了什么吗??

谢谢

试试这个

 var doubletapgesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "processDoubleTap:")
        doubletapgesture.numberOfTapsRequired = 1
        collectionView.addGestureRecognizer(doubletapgesture)

现在处理手势

func processDoubleTap (sender: UITapGestureRecognizer)
    {
        if sender.state == UIGestureRecognizerState.Ended
        {
            var point:CGPoint = sender.locationInView(collectionView)
            var indelPath:NSIndexPath =collectionView.indexPathForItemAtPoint(point)
            if indexPath
            {
                println("image taped")
            }
            else
            {
               //Do Some Other Stuff Here That Isnt Related;
            }
        }
    }