user10011979
user10011979

Reputation:

How do I animate UICollectionViewCells using Swift?

I am currently trying to get my cells to animate into the storyboard one at a time.

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    cell.alpha = 0
    cell.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5)
    UIView.animate(withDuration: 1.0, animations: { () -> Void in
        cell.alpha = 1
        cell.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1)
    })
}

This is the code that I am currently using. However, it only allows me to run the animation on all of the cells at once. What I would like is to have cell 1, 2, 3, and 4 to animate in that order. Any ideas on how I could go about this? If there is a better way then please go tell me if there is.

Upvotes: 0

Views: 2592

Answers (3)

user10011979
user10011979

Reputation:

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    cell.alpha = 0
    cell.layer.transform = CATransform3DMakeRotation(10, 1, 1, 0)
    UIView.animate(withDuration: 1.0, delay: 1.0*Double(indexPath.row),animations: { () -> Void in
    cell.alpha = 1

        cell.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1)
    })
}

After adding

 delay: 1.0*Double(indexPath.row)

it solved my problem and now the view animates them in order. @Syed Faraz Haider Zaidi

Upvotes: 2

Vitaliy Rusinov
Vitaliy Rusinov

Reputation: 266

I recommend you create some view with ordered animation and use only UIView not UICollectionView. When animation is completed u can show your real UICollectionView.

Upvotes: 0

Syed Faraz Haider Zaidi
Syed Faraz Haider Zaidi

Reputation: 1367

you can use repeat and autoreverse options.

UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse], animations: {
  cell.alpha = 1
        cell.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1)

}, completion: nil)

Upvotes: 0

Related Questions