0

I have the below code which successfully causes a cell in a collectionView to depress when tapped.

The issue is that one must hold for pretty long before the depression happens. Don't get me wrong. It's only about 1 second, but if you were to compare that speed to the speed of snapchat you can clearly see snapchat's is almost instant.

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {

     UIView.animate(withDuration: 0.1, animations: {

         collectionView.cellForItem(at: indexPath)!.transform = CGAffineTransform.identity.scaledBy(x: 0.95, y: 0.95)

      })
 }

Question: How could I make this animation for each cell, be instant or closer to it, like snapchat?

Update:

This seems to be doing it:

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {

    UIView.animate(withDuration: 0.1, animations: {

        collectionView.cellForItem(at: indexPath)!.transform = CGAffineTransform.identity.scaledBy(x: 0.95, y: 0.95)

    }) { (true) in

        UIView.animate(withDuration: 0.1, animations: {

            collectionView.cellForItem(at: indexPath)!.transform = CGAffineTransform.identity.scaledBy(x: 1, y: 1)

        })

    }

}

This is an option if anyone wants something basic.

Trevor Reid
  • 2,533
  • 3
  • 23
  • 33

2 Answers2

3

Disable the collectionView content touch delay in interface builder:

Delay

or in code:

myCollectionView.delaysContentTouches = false

Tip: enabling .beginFromCurrentState option for this animation will cause it looks more fluid and responsive. From Apple's docs on beginFromCurrentState:

Start the animation from the current setting associated with an already in-flight animation.

UIView.animate(withDuration: 0.1, delay: 0, options: [.beginFromCurrentState], animations: { 
}) { finished in 
}
Mojtaba Hosseini
  • 47,708
  • 12
  • 157
  • 176
0

If you want instantness you can transform to and back in touchesBegan and touchesEnded/touchesCanceled methods of your custom UICollectionViewCell

arturdev
  • 10,228
  • 2
  • 34
  • 62