Hi Claude,
Thanks for taking the time to respond, much appreciated.
The animation is activated by the didselect function as in your sample code.
I should clarify that unlike your animation, my animation does not return to its original value, unless another event happens. so its state remains, and this is reflected in other cells off screen if you scroll, which I am pretty sure is normal behaviour for reused cells?
For clarity here is a cut down version of my controller and cell Code.
class ViewController: UIViewController {
var collectionView: UICollectionView!
var cellScale : CGFloat = 0.67
override func viewDidLoad() {
super.viewDidLoad()
let screenSize = UIScreen.main.bounds.size
let cellWidth = floor(screenSize.width * cellScale )
let cellHeight = floor(screenSize.height * cellScale)
let view = UIView()
view.backgroundColor = .white
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: cellWidth, height: cellHeight )
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView?.register(customCell.self, forCellWithReuseIdentifier: "customCell")
collectionView?.backgroundColor = UIColor.white
collectionView?.dataSource = self
collectionView?.delegate = self
view.addSubview(collectionView ?? UICollectionView())
self.view = view
}
}
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) - Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) - UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! customCell
cell.labelRow1.text = String(indexPath.row + 1)
return cell
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row + 1)
let cell = collectionView.cellForItem(at: indexPath) as! customCell
UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseOut, animations: {
cell.iv.center.y = cell.isSelected ? 1 : -1
cell.transform = cell.isSelected ? CGAffineTransform(scaleX: 1.1, y: 1.1) : CGAffineTransform.identity
}, completion: nil)
}
}
CELL CODE
import Foundation
import UIKit
class customCell: UICollectionViewCell {
override func prepareForReuse() {
super.prepareForReuse()
}
let iv = UIImageView()
let labelRow1 = UILabel()
override init(frame: CGRect) {
super.init(frame: .zero)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension customCell {
func configure() {
print("configuring custom cell")
labelRow1.translatesAutoresizingMaskIntoConstraints = false
labelRow1.backgroundColor = .lightGray
labelRow1.textAlignment = .center
labelRow1.clipsToBounds = true
labelRow1.layer.borderWidth = 0.6
labelRow1.layer.borderColor = UIColor.systemGray.cgColor
labelRow1.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
contentView.addSubview(labelRow1)
iv.translatesAutoresizingMaskIntoConstraints = false
iv.backgroundColor = .blue
iv.isUserInteractionEnabled = true
iv.clipsToBounds = true
iv.layer.cornerRadius = 12
iv.layer.borderColor = UIColor.systemGray.cgColor
iv.layer.borderWidth = 0.6
iv.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
contentView.addSubview(iv)
NSLayoutConstraint.activate([
iv.topAnchor.constraint(equalTo: contentView.topAnchor),
iv.leftAnchor.constraint(equalTo: contentView.leftAnchor),
iv.rightAnchor.constraint(equalTo: contentView.rightAnchor),
iv.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.55),
labelRow1.topAnchor.constraint(equalTo: iv.bottomAnchor),
labelRow1.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
labelRow1.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
labelRow1.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.15),
])
}
}
Topic:
UI Frameworks
SubTopic:
UIKit
Tags: