Do the animation in didSelectItemAt.
Here is an example where you would just zoom the cell, without changing the size
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedCollectionItem = indexPath.row
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 3.0, delay: 0.0, options: .curveEaseOut, animations: {
cell?.transform = cell!.isSelected ? CGAffineTransform(scaleX: 1.5, y: 1.5) : CGAffineTransform.identity
}, completion: nil)
}
Here a complete example I tested:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedCollectionItem = indexPath.row
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseOut, animations: {
cell?.layer.zPosition = cell!.isSelected ? 1 : -1
cell?.transform = cell!.isSelected ? CGAffineTransform(scaleX: 1.5, y: 1.5) : CGAffineTransform.identity
}, completion: { (done) in self.performSegue(withIdentifier: "ColorSegue", sender:self) }) // Once animated, can segue
}
selectedCollectionItem = indexPath.row
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {
cell?.layer.zPosition = cell!.isSelected ? 1 : -1
cell?.transform = cell!.isSelected ? CGAffineTransform(scaleX: 1.5, y: 1.5) : CGAffineTransform.identity
}, completion: nil)
}