我将集合视图单元重用为自定义单选按钮。作为单选按钮选择,我使用两个视图:
标记视图已隐藏并缩放。它基本上是简单的选择。
在RadioButtonCollectionViewCell
:
var checkMark: UIView = {
let view = UIView()
view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
view.backgroundColor = .darkGreyFts
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override var isSelected: Bool {
get {
return super.isSelected
} set {
super.isSelected = newValue
isSelected ? checkMark.scaleAnimate(1) : checkMark.scaleAnimate(-1)
}
}
override func prepareForReuse() {
super.prepareForReuse()
self.infoLabel.text = nil
self.checkMark.isHidden = false
}
UIView扩展名:
func scaleAnimate(_ state: Int) {
switch state {
case 1:
self.isHidden = false
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.transform = CGAffineTransform(scaleX: 1, y: 1)
})
case -1:
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}, completion: { _ in
self.isHidden = true
})
default:
break
}
}
// MARK: - UICollectionView delegate.
extension DriverFormsViewController : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? RadioButtonCollectionViewCell {
cell.isSelected = true
collectionCellSelectedAtIndexPath = indexPath
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? RadioButtonCollectionViewCell {
cell.isSelected = false
}
}
}
// MARK: - UICollectionView data source.
extension DriverFormsViewController : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return optionsDictionary.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! RadioButtonCollectionViewCell
let key = optionsKeys[indexPath.row]
cell.infoLabel.text = optionsDictionary[key]
cell.checkMark.isHidden = true
if let collectionCellIsSelectedAtIndexPath = collectionCellSelectedAtIndexPath {
if collectionCellIsSelectedAtIndexPath == indexPath {
cell.checkMark.scaleAnimate(1)
}
}
return cell
}
}
我如何能够重用隐藏/未缩放的视图?每当我向下滚动并返回到集合时,选择的单元格未被选中 - 意味着,复选标记被隐藏,每当我点击此单元格时,它都没有动画。什么想法可能是错的?
提前致谢!
答案 0 :(得分:1)
您必须确保在选择新单元格之前取消选择当前单元格
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let currentSelectedIndexPath = collectionCellSelectedAtIndexPath,
let currentCell = cellForItem(at: currentSelectedIndexPath) as? RadioButtonCollectionViewCell {
currentCell.isSelected = false
}
if let cell = collectionView.cellForItem(at: indexPath) as? RadioButtonCollectionViewCell {
cell.isSelected = true
collectionCellSelectedAtIndexPath = indexPath
}
}
而不是使用prepareForReuse
我会在else
cellForItemAt
子句
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! RadioButtonCollectionViewCell
let key = optionsKeys[indexPath.row]
cell.infoLabel.text = optionsDictionary[key]
cell.checkMark.isHidden = true
if let collectionCellIsSelectedAtIndexPath = collectionCellSelectedAtIndexPath,
collectionCellIsSelectedAtIndexPath == indexPath {
cell.checkMark.scaleAnimate(1)
} else {
cell.checkMark.scaleAnimate(-1)
}
return cell
}