gpt4 book ai didi

ios - 如何在选择 Swift 3.0 时缩放 UICollectionViewCell?

转载 作者:可可西里 更新时间:2023-11-01 05:25:10 25 4
gpt4 key购买 nike

enter image description here

我在这里尝试突出显示选择的 UICollectionViewCell,如图所示。我尝试将边框添加到所选单元格,并且边框位于单元格内容 View 内。这是我的尝试:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
let borderWidth: CGFloat = 6
cell?.contentView.frame = (cell?.labelBackground.frame.insetBy(dx: +borderWidth, dy: +borderWidth))!
cell?.contentView.layer.borderColor = cell?.backgroundColor?.cgColor
cell?.contentView.layer.borderWidth = borderWidth
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
let borderWidth: CGFloat = 0
cell?.contentView.frame = (cell?.labelBackground.frame.insetBy(dx: +borderWidth, dy: +borderWidth))!
cell?.contentView.layer.borderColor = UIColor.clear.cgColor
cell?.contentView.layer.borderWidth = borderWidth

}

如何做到这一点?

最佳答案

不是为所选单元格添加边框宽度,而是使用变换比例缩放所选单元格。在 didSelect 中写入这段代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
priorityCollectionView.bringSubview(toFront: selectedCell!)

UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations: {
selectedCell?.transform = CGAffineTransform(scaleX: 1.2, y: 2)
})
}

在 didDeselect 中:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let unselectedCell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell
UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations: {
unselectedCell?.transform = .identity
})
}

结果:enter image description here

关于ios - 如何在选择 Swift 3.0 时缩放 UICollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46827536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com