tap gesture on uiimage in collectionview

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 02:35:40

问题


In each cell of my UICollectionView, I have multiple object to interact with. So instead of use didSelect delegate method, I really wanted to add a tap gesture on each object of the cell.

To make it simple, I removed all the other objects in the example:

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

}

In viewDidLoad, I use a nib :

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

UICollectionView Settings:

  • Delays Content Touches: True
  • Cancellable Content Touches: True

With this example, I can't handle the tap gesture. Nothing happen. Did I miss something??

Thanks


回答1:


try this one

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

now handle gesture

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


来源:https://stackoverflow.com/questions/32459715/tap-gesture-on-uiimage-in-collectionview

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