Search code examples
iosswiftuicollectionviewcelltap

How to reduce the amount of time between a tap on a cell and the running of certain code?


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.


Solution

  • 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 
    }