Swift 在 UIcollectionViewCells 上画线
Swift Draw lines on UIcollectionViewCells
我可以在集合视图单元格的每个单元格上画线,但是当移动到第三个单元格时,我再次在这个单元格上找到线
我只想为我在其上绘制的每个单元格绘制线条,以便再次在第三个单元格上重复任何帮助,请
绘图canvas
这是我的自定义 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
}
如果我没理解错的话,你可以在每个单元格上画画,但你的画却出现在你没有画过的其他单元格上?如果是这样,那么您需要在重新使用之前清除单元格的绘图。在你的 ImagePreviewcollectionView
class 中,我假设你有一个 Canvas
对象,你需要在单元格中实现 override func prepareForReuse() {
并重置 canvas 上的绘图,所以它是当 tableView 重复使用单元格时为空白。
我通过在包含绘图点的数组中的任何单元格上保存绘图解决了我的错误
在 cellForItemAt
func 中,我再次重画线条
我可以在集合视图单元格的每个单元格上画线,但是当移动到第三个单元格时,我再次在这个单元格上找到线
我只想为我在其上绘制的每个单元格绘制线条,以便再次在第三个单元格上重复任何帮助,请
绘图canvas 这是我的自定义 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
}
如果我没理解错的话,你可以在每个单元格上画画,但你的画却出现在你没有画过的其他单元格上?如果是这样,那么您需要在重新使用之前清除单元格的绘图。在你的 ImagePreviewcollectionView
class 中,我假设你有一个 Canvas
对象,你需要在单元格中实现 override func prepareForReuse() {
并重置 canvas 上的绘图,所以它是当 tableView 重复使用单元格时为空白。
我通过在包含绘图点的数组中的任何单元格上保存绘图解决了我的错误
在 cellForItemAt
func 中,我再次重画线条