【问题标题】:color of cgpoint changes all lines and should only change new linescgpoint 的颜色会更改所有行,并且应该只更改新行
【发布时间】:2019-10-27 22:50:10
【问题描述】:

我的代码用于类。当函数 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()
}

}

【问题讨论】:

    标签: swift subclass cgpoint uigraphicscontext didset


    【解决方案1】:

    您需要创建一个struct 来存储颜色与线条。在touchesBegan() 中,将当前strokeColorcoloredLine 一起存储。在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()
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 2017-09-16
      相关资源
      最近更新 更多