1
這是我的代碼:如何使用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值。
你該死的權利!有用! –