swift - cgpoint 的颜色会更改所有行,并且只应更改新行

标签 swift subclass cgpoint uigraphicscontext didset

我的代码用于类。当函数 dizzy 被调用时,它会改变 uiview 中所有线条的颜色。我想要它做的只是更改调用函数后绘制的线条颜色。它不应该像现在一样改变已经绘制的线条的颜色。

class ViewController: UIViewController {
@objc func dizzy() {
     canvas.strokeColor = .gray

}

 var canvas = Canvas()
 }
  class Canvas: UIView {

    var strokeColor = UIColor.green {
          didSet {
              self.setNeedsDisplay()
          }
      }

func undo() {
    _ = lines.popLast()
    setNeedsDisplay()
}

func clear() {
    lines.removeAll()
    setNeedsDisplay()
}




var lines = [[CGPoint]]()

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

    guard let context = UIGraphicsGetCurrentContext() else { return }

   context.setStrokeColor(strokeColor.cgColor)
    context.setLineWidth(5)
    context.setLineCap(.butt)

    lines.forEach { (line) in
        for (i, p) in line.enumerated() {
            if i == 0 {
                context.move(to: p)
            } else {
                context.addLine(to: p)
            }
        }
    }

    context.strokePath()

}

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

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let point = touches.first?.location(in: self) else { return }
    guard var lastLine = lines.popLast() else { return }
    lastLine.append(point)
    lines.append(lastLine)
    setNeedsDisplay()
}

}

最佳答案

您需要创建一个结构来存储线条的颜色。在 touchesBegan() 中,使用 coloredLine 存储当前的 linesColor。在draw(rect:)中,为每条线设置context.setStrokeColor(line.color.cgColor),然后再抚摸该线。

我通过添加按钮来更改颜色来测试这一点。您可以根据需要将其连接起来。

struct ColoredLine {
    var color = UIColor.black
    var points = [CGPoint]()
}


class ViewController: UIViewController {

    @IBOutlet weak var canvas: Canvas!

    @IBAction func doRed(_ sender: UIButton) {
        canvas.strokeColor = .red
    }

    @IBAction func doGreen(_ sender: UIButton) {
        canvas.strokeColor = .green
    }

    @IBAction func doBlue(_ sender: UIButton) {
        canvas.strokeColor = .blue
    }

    @IBAction func doBlack(_ sender: UIButton) {
        canvas.strokeColor = .black
    }

 }


class Canvas: UIView {

    var strokeColor = UIColor.green

    func undo() {
        _ = lines.popLast()
        setNeedsDisplay()
    }

    func clear() {
        lines.removeAll()
        setNeedsDisplay()
    }

    var lines = [ColoredLine]()

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

        guard let context = UIGraphicsGetCurrentContext() else { return }

        context.setLineWidth(5)
        context.setLineCap(.butt)

        lines.forEach { (line) in
            for (i, p) in line.points.enumerated() {
                if i == 0 {
                    context.move(to: p)
                } else {
                    context.addLine(to: p)
                }
            }

            context.setStrokeColor(line.color.cgColor)
            context.strokePath()
            context.beginPath()
        }


    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        var coloredLine = ColoredLine()
        coloredLine.color = strokeColor
        lines.append(coloredLine)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let point = touches.first?.location(in: self) else { return }
        guard var lastLine = lines.popLast() else { return }
        lastLine.points.append(point)
        lines.append(lastLine)
        setNeedsDisplay()
    }

}

关于swift - cgpoint 的颜色会更改所有行,并且只应更改新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584028/

相关文章:

swift - 如何在 swift 中更改子类中的属性值?

java - 如何使用现有基类对象构造派生类

ios - 如何在许多子类中使用 UITableViewDelegate 对 UIViewController 进行子类化

swift - 即使父节点正在移动,如何为每个节点在屏幕上提供相同的起始位置?

ios - 在 didSelectItemAt 中,单元格的委托(delegate)为零

ios - Swift - 是否可以 ping RTMP Url?

objective-c - Objective-C - 如果在 CGRectmake 上声明?

objective-c - 将 CGPoint 传递给另一个方法以 (inf, inf) 结尾

ios - Realm swift : required public init() error

ios - Swift 3,显示一些 HTML 文本行