簡體   English   中英

如何使用Swift中的UILongPressGestureRecognizer通過tag屬性傳遞indexPath.row?

[英]How to pass indexPath.row through the tag property with UILongPressGestureRecognizer in Swift?

這是我的代碼:

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沒有tag屬性,但.view屬性有它,就像你在代碼中看到的那樣。

我已經將makeItCoolAction發送方從UILongPressGestureRecognizer更改為UIGestureRecogizer,它仍然保持打印0值。

此時視圖為零:

coolGesture.view?.tag = 4

添加手勢后嘗試設置水龍頭,它有如下視圖:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM