ios – 如何在选择Swift 3.0上缩放UICollectionViewCell?

前端之家收集整理的这篇文章主要介绍了ios – 如何在选择Swift 3.0上缩放UICollectionViewCell?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

enter image description here

在这里,我正在尝试突出显示选择的UICollectionViewCell,如图所示.我尝试将边框添加到选定的单元格,边框位于单元格内容视图中.这是我的尝试:

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,animations: {
        unselectedCell?.transform = .identity
    })
}

结果:

enter image description here

猜你在找的Xcode相关文章