简体   繁体   中英

Swift Draw lines on UIcollectionViewCells

I can draw lines on each cell of the collection view cell but when moving to the third cell I find lines again on this cell

I want to make lines draw only for each cell that I drew on it on to repeat on the third cell again any help, please

Drawing canvas this is my custom UIView with an override draw function to draw lines I saved on it

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

this is my code in collection view to add the cell item in the collection view

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
}

I solved my error with saving draw on any cell in an array contains drawing points and in cellForItemAt func I redraw lines again

If I'm understanding correctly, you are able to draw on each cell, but your drawings are appearing on other cells that you have not drawn on? If so, then you need to clear the drawing for the cell before it's being reused. In your ImagePreviewcollectionView class I assume you have a Canvas object, you need to implement override func prepareForReuse() { in the cell and reset the drawing on that canvas so it is blank when the tableView reuses the cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM