UIcollectionViewCells上的快速绘制线

问题描述 投票:1回答:1

我可以在集合视图单元的每个单元上画线,但是当移到第三个单元时,我再次在该单元上找到线

[我只想为我绘制的每个单元格绘制线条,以便在第三个单元格上再次重复任何帮助,请

绘图画布这是我的自定义UIView,带有重写绘制功能来绘制我保存在上面的线条

class Canvas: UIView {

    var lines = [drawLine]()
    var currentColor : UIColor  = UIColor.red

    override func draw(_ rect: CGRect) {
        // custom drawing
        super.draw(rect)

        guard let context = UIGraphicsGetCurrentContext() else { return }


        lines.forEach { (line) in
            context.setStrokeColor(line.color.cgColor)
            context.setLineWidth(10)
            context.setLineCap(.round)
            for (i, p) in line.points.enumerated() {
                if i == 0 {
                    context.move(to: p)
                } else {
                    context.addLine(to: p)
                }
            }
            context.strokePath()
        }
    }

    func changeDrawColor(color:UIColor) {

        self.currentColor = color

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        lines.append(drawLine.init(color: currentColor, points: []))
    }

    // track the finger as we move across screen
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let point = touches.first?.location(in: nil) else { return }
               print("point => \(point)")

        guard var lastLine = lines.popLast() else { return }
        lastLine.points.append(point)
        lines.append(lastLine)
        setNeedsDisplay()
    }
}

cellForItemAt

这是我在集合视图中的代码,用于在集合视图中添加单元格项

if(collectionView == self.previewCollectionView ){
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImagePreviewcollectionView.identifier, for: indexPath) as! ImagePreviewcollectionView
    cell.ImageView.image = UIImage(named: String(indexPath.row))
    cell.ImageView.layer.cornerRadius = 10
    cell.ImageView.layer.masksToBounds = true
    cell.layer.borderColor = UIColor.red.cgColor
    return cell
}
ios swift uiview uicollectionview uicollectionviewcell
1个回答
0
投票

如果我的理解正确,您可以在每个单元格上绘制,但是您的图形显示在尚未绘制的其他单元格上吗?如果是这样,那么您需要先清除该单元的图形,然后再使用它。在您的ImagePreviewcollectionView类中,我假设您有一个Canvas对象,您需要在单元格中实现override func prepareForReuse() {并重置该画布上的图形,以便在tableView重用该单元格时它是空白的。

© www.soinside.com 2019 - 2024. All rights reserved.