如何使用Swift中的UILongPressGestureRecognizer通过tag属性传递indexPath.row?

如何使用Swift中的UILongPressGestureRecognizer通过tag属性传递indexPath.row?

问题描述:

这是我的代码:如何使用Swift中的UILongPressGestureRecognizer通过tag属性传递indexPath.row?

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

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

    let coolGesture = UILongPressGestureRecognizer(target: self, action: #selector(detailedPerson.makeItCoolAction(_:))) 

    coolGesture.minimumPressDuration = 0.5 
    coolGesture.view?.tag = 4 

    cell.addGestureRecognizer(coolGesture) 

这是函数代码:

func makeItCoolAction(sender: UIGestureRecognizer){ 

    print(sender.view?.tag) 
    print("Photo cooled") 

} 

控制台打印相同的值:0,这是默认的。我强烈地需要将indexPath.row值传递给makeItCoolAction,但我看到UIGestureRecognizer没有标签属性,但.view属性具有它,就像您在代码中看到的那样。

我已将makeItCoolAction发件人从UILongPressGestureRecognizer更改为UIGestureRecogizer,并且它仍然保持打印0值。

的看法是零,在这一点上:

coolGesture.view?.tag = 4 

尝试设置水龙头,你已经添加了手势之后,它有这样的观点:

cell.addGestureRecognizer(coolGesture) 
coolGesture.view?.tag = 4 
+0

你该死的权利!有用! –