A. Sola A. Sola - 1 year ago 108
Swift Question

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

This is my code:

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


And this is the function code:

func makeItCoolAction(sender: UIGestureRecognizer){

print("Photo cooled")


The console prints the same value: 0, which is the default one. I strongly need to pass the indexPath.row value to the makeItCoolAction but I saw UIGestureRecognizer doesn't have tag property but .view property has it as you may see in the code.

I've changed the makeItCoolAction sender from UILongPressGestureRecognizer to UIGestureRecogizer and it still keep printing the 0 value.

Answer Source

view is nil at this point:

coolGesture.view?.tag = 4

try setting the tap after you've added the gesture and it has a view like this:

coolGesture.view?.tag = 4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download