gpt4 book ai didi

swift - 设备方向更改后使 collectionViewCell 中的 CAShapeLayer 无效并重绘

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:17 25 4
gpt4 key购买 nike

我有一个自定义 Collection View 单元格,它使用以下代码向单元格层添加虚线边框:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = appDealCollectionView.dequeueReusableCell(withReuseIdentifier: "appDealCollectionCell", for: indexPath) as! AppDealCollectionViewCell
if let codes = discountCodes {
cell.discountCodeTitle.text = codes[indexPath.row].codeMessageOne
cell.discountCode.text = codes[indexPath.row].code

let yourViewBorder = CAShapeLayer()
yourViewBorder.strokeColor = UIColor.black.cgColor
yourViewBorder.lineWidth = 2
yourViewBorder.lineDashPattern = [10, 10]
yourViewBorder.frame = cell.bounds
yourViewBorder.fillColor = nil
yourViewBorder.path = UIBezierPath(roundedRect: cell.bounds, cornerRadius: 6).cgPath
cell.layer.addSublayer(yourViewBorder)
}
return cell
}

此代码在 View 的初始加载时工作得很好。但是,当方向改变时,单元格大小也会改变。上面的代码确实正确地绘制了新的边框 CAShapeLayer,但是之前绘制的边框图层仍然存在,它是根据旧尺寸绘制的。

结果是两个不同的边界层同时出现,以不同的尺寸相互重叠。

如何使之前绘制的 CAShapeLayers 失效?无效在哪里完成?在 cellForItemAt 中?或者可能在自定义“AppDealCollectionViewCell”本身内部?

最佳答案

由于单元格是可重用的,每次调用 cellForRowAtIndexPath 都会将另一个 CAShapeLayer 实例添加到单元格中。这就是为什么您有几个边界相互重叠的原因。此外,CALayer 既不支持自动布局也不支持 autoresizingMask,因此您必须手动更新 CAShapeLayer 的大小。

您应该创建 UITableViewCell 的子类,然后创建 CAShapeLayer 的实例并将指向它的指针存储在类属性变量中。一旦发生布局循环,在 layoutSubviews 函数中,您需要更新 CAShapeLayer 的框架。

最终的实现是这样的:

class BorderedTableViewCell: UITableViewCell {

lazy var borderLayer = CAShapeLayer()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupBorderLayer()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupBorderLayer()
}

private func setupBorderLayer() {
borderLayer.strokeColor = UIColor.black.cgColor
borderLayer.lineWidth = 2
borderLayer.fillColor = nil
borderLayer.lineDashPattern = [10, 10]
layer.addSublayer(borderLayer)
}

private func updateBorderLayer() {
borderLayer.frame = bounds
borderLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 6).cgPath
}

override func layoutSubviews() {
super.layoutSubviews()
updateBorderLayer()
}
}

希望对您有所帮助。

关于swift - 设备方向更改后使 collectionViewCell 中的 CAShapeLayer 无效并重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51198788/

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