加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:51:12 所属栏目:百科 来源:网络整理
导读:在这里,我正在尝试突出显示选择的UICollectionViewCell,如图所示.我尝试将边框添加到选定的单元格,边框位于单元格内容视图中.这是我的尝试: func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) { let cell = p

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读