如何添加颜色按钮快速绘图应用程序

时间:2018-01-26 12:33:42

标签: swift drawing

编辑:我在课堂上添加了var,而不是在UIview中通过按钮更改了它。

在我的班级画布中:

`lineColor = color`

我的课外,但在画布文件中     color = UIColor.black

在viewcontroller中

@IBAction func blue(_ sender: Any) { color = UIColor.blue }

我正在创建一个快速绘图应用程序。我为绘图(画布)制作了特殊视图,现在我创建了一个更改颜色的按钮,但它没有与另一个文件中的canvas类进行通信。我试图将func添加到画布,而不是通过按钮调用它,但它不起作用。有谁可以帮助我吗?

这是我的canvas.swift文件

func blue(){

}

 class Canvas: UIView {


var lineColor:UIColor!
var lineWidth:CGFloat!
var path:UIBezierPath!
var touchPoint:CGPoint!
var startingPoint:CGPoint!


override func layoutSubviews() {
    self.clipsToBounds = true
    self.isMultipleTouchEnabled = false
    lineColor = UIColor.black
    func blue(){
        lineColor = UIColor.blue
    }

    lineWidth = 7

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    startingPoint = touch?.location(in: self)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    touchPoint = touch?.location(in: self)

    path = UIBezierPath()
    path.move(to: startingPoint)
    path.addLine(to: touchPoint)
    startingPoint = touchPoint

    drawShapeLayer()
}


func drawShapeLayer(){
let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = lineColor.cgColor
    shapeLayer.lineWidth = lineWidth
    shapeLayer.fillColor = UIColor.clear.cgColor
    self.layer.addSublayer(shapeLayer)
    self.setNeedsDisplay()


}
func clearCanvas(){
    path.removeAllPoints()
    self.layer.sublayers = nil
    self.setNeedsDisplay()
}

1 个答案:

答案 0 :(得分:0)

您可以通过NSUserDefault传递此颜色。

保存:

UserDefaults.standard.set(color, forKey: "colorKey")

收到:

UserDefaults.standard.object(forKey: "colorKey")