在collectionview中的uiimage上点击手势

时间:2015-09-08 13:55:33

标签: ios swift uicollectionview uitapgesturerecognizer

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中,我使用了一个nib:

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

UICollectionView设置:

  • 延迟内容触动:真实
  • 可取消内容触动:真实

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

由于

1 个答案:

答案 0 :(得分:2)

尝试这个

 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;
            }
        }
    }