У меня есть вопрос о том, как я могу управлять анимацией изменения размера UICollectionViewCell.Чтобы быть более понятным, я хотел бы знать, как настроить продолжительность и кривую анимации.У меня есть простой UICollectionView с макетом потока и ячейкой, которая содержит кнопку.Нажатие на кнопку вызывает изменение высоты ячейки.
Вот код, который у меня есть:
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)
}
}
А вот код моей ячейки
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)
}
КакВы можете видеть, что я решил сделать анимации довольно длинными для этого примера, и поэтому заметил, что блок UIView.animate на самом деле не влияет на анимацию изменения размера ячейки.Я думаю, что я должен искать CAAnimation или что-то подобное.Был бы рад услышать любые идеи!Спасибо.