点击按钮时的图层

layer when tap button

我有这段代码,其中我将 "ColorCell" class 留在我的单元格中,之后我想点击我的 collectio 的一个按钮,我创建了 ul layes around (only the selected一)获得一个想法并知道哪个premo。代码有效,在函数内调试但不执行它,它只运行 tapColor

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ColorCell.cellId, for: indexPath) as! ColorCell
        if cell.isSelected {
            cell.locationImage.layer.borderWidth = 1
            cell.locationImage.layer.borderColor = UIColor.white.cgColor
            self.delegate?.tapColor(colorRound: datasource[indexPath.item].color)
        }
    }
    class ColorCell: UICollectionViewCell {
    static let reuseIdentifier = "TextColorRoundCell_RID"
    static let cellId = "ImageSliderCell__"
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    var cellID: String?
    var locationImage = UIImageView().then{
        [=10=].contentMode = UIView.ContentMode.scaleAspectFit
        [=10=].layer.cornerRadius = (((UIScreen.main.bounds.size.width/8)*0.8)/2)
//        [=10=].layer.borderWidth = 1
//        [=10=].layer.borderColor = UIColor.white.cgColor
    }

    func setupViews(){
        self.addSubview(self.locationImage)
        self.locationImage.activate([
            locationImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.8),
            locationImage.heightAnchor.constraint(equalTo: self.locationImage.widthAnchor),
            locationImage.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            locationImage.centerYAnchor.constraint(equalTo: self.centerYAnchor),
            ])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Example

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ColorCell.cellId, for: indexPath) as! ColorCell
    if cell.isSelected {
        cell.locationImage.layer.borderWidth = 1
        cell.locationImage.layer.borderColor = UIColor.white.cgColor
        self.delegate?.tapColor(colorRound: datasource[indexPath.item].color)
    }



    for singleCell in collectionView.visibleCells {
        if singleCell != cell {
            singleCell.layer.borderWidth = 0
            singleCell.layer.borderColor = UIColor.clear
        }
    }
    collectionView.reloadData() 
}

试试这个,ci有很多方法可以解决这个问题

编辑 2

class ColorCell: UICollectionViewCell {
    //Qui dentro inserisci tutti i componenti della cella

    //var label: UILabel
    //var image: UIImage etc..

    override var isSelected: Bool {
        didSet{
            if isSelected {
            //QUI cambi i colori degli elementi
            } else {
            //QUI resetti i colori come erano all'inizio
            }
        }
    }
}