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

时间:2016-04-27 19:37:02

标签: swift uigesturerecognizer nsindexpath uilongpressgesturerecogni

这是我的代码:

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值。

1 个答案:

答案 0 :(得分:0)

此时视图为零:

coolGesture.view?.tag = 4

尝试在添加手势后设置点按,它有如下视图:

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