1

I have a question about the way I can control UICollectionViewCell's resize animation. To be more clear I'd like to know how to set up animation's duration and curve. I have a simple UICollectionView with flow layout and a cell which contains a button. Tap on the button causes a cell's height change.

Here is the code I have:

class ViewController: UICollectionViewController {
    var isExpanded = false
    var cv: UICollectionView {
        get {
            return collectionView!
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.estimatedItemSize = CGSize(width: collectionView!.frame.width, height: 400.0)
        }

        collectionView?.delegate = self
        collectionView?.dataSource = self
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
        cell.parent = self
        return cell
    }
}

    extension ViewController: UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell
        /*
         Need to calculate cell's height.
         */
        return isExpanded ? CGSize(width: collectionView.frame.width, height: 400) : CGSize(width: collectionView.frame.width, height: 200)
    }
}

And here is the code of my cell

class CollectionViewCell: UICollectionViewCell {
    var heightConstraint: NSLayoutConstraint!
    var isExpanded = false
    var parent: ViewController! {
        didSet {
          contentView.widthAnchor.constraint(equalToConstant: parent.view.frame.width).isActive = true
          heightConstraint = NSLayoutConstraint(item: contentView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 200.0)
          contentView.addConstraint(heightConstraint)
        }
    }

    @IBOutlet weak var height: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.translatesAutoresizingMaskIntoConstraints = false
    }

    @IBAction func expand(_ sender: Any) {
        self.parent.cv.performBatchUpdates({
            //This UIView animate block doesn't matter anything actually.
            UIView.animate(withDuration: 10.5, animations: {[unowned self] in
            self.isExpanded = !self.isExpanded
            self.heightConstraint.constant = self.isExpanded ? 400.0 : 200.0
            self.layoutIfNeeded()
            }, completion: nil)
    }, completion: nil)
}

As you can see I decided to make animations pretty long for this example, and because of that, I've noticed that UIView.animate block actually doesn't affect cell's resize animation. I guess I should look for CAAnimation or something similar. Would be glad to hear any ideas! Thank you.

Mikhail Glotov
  • 336
  • 3
  • 9

1 Answers1

0

To achieve this you need to extend UICollectionViewLayoutAtteributes by adding property of type CABasicAnimation.

This is well described in This answer

fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30