cgpoint的颜色会更改所有行,并且只能更改新行 [英] color of cgpoint changes all lines and should only change new lines

查看:97
本文介绍了cgpoint的颜色会更改所有行,并且只能更改新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My code uses to classes. When the function dizzy is called it changes the color of all the lines in the uiview. What I want it to do is only change lines colors that are drawn after the function is called. It should not change the color of the lines that are already drawn like it does now.

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()
}

}

推荐答案

您需要创建一个struct来用行存储颜色.在touchesBegan()中,将当前的strokeColorcoloredLine存储在一起.在draw(rect:)中,对于每行,在描边之前设置context.setStrokeColor(line.color.cgColor).

You need to create a struct to store the color with the line. In touchesBegan(), store the current strokeColor with the coloredLine. In draw(rect:), for every line set the context.setStrokeColor(line.color.cgColor) before stroking the line.

我通过添加按钮来更改颜色进行了测试.您可以根据需要将其连接起来.

I tested this by adding buttons to change the colors. You can hook it up as you see fit.

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()
    }

}

这篇关于cgpoint的颜色会更改所有行,并且只能更改新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆