在 collectionview 中的 uiimage 上点击手势 [英] tap gesture on uiimage in collectionview

查看:28
本文介绍了在 collectionview 中的 uiimage 上点击手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

在 viewDidLoad 中,我使用笔尖:

In viewDidLoad, I use a nib :

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

UICollectionView 设置:

UICollectionView Settings:

  • 延迟内容接触:真实
  • 可取消的内容涉及:真实

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

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

谢谢

推荐答案

试试这个

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

这篇关于在 collectionview 中的 uiimage 上点击手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆