通过按钮和撤消按钮只绘制一个对象

draw only one object by button and undo button

我是 swift 的初学者,现在我正在为可能很简单的事情而奋斗。

如果我点击撤消按钮,MyDrawView 应该是清晰的,如果我点击多边形填充,例如紫色,然后我点击圆形,只有一个对象。

像这样:

但现在我有了这个:

我的代码:

import UIKit
import QuartzCore

class MyDrawView: UIView{

    var circle = UIBezierPath()
    let polygon = UIBezierPath()

    let shapeLayer1 = CAShapeLayer()
    let shapeLayer2 = CAShapeLayer()


    func drawCircle(){
        circle = UIBezierPath(arcCenter: CGPoint(x: 200, y: 300), radius: CGFloat(100), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)

        shapeLayer1.path = circle.cgPath

        shapeLayer1.fillColor = UIColor.clear.cgColor
        shapeLayer1.strokeColor = UIColor.black.cgColor 
        shapeLayer1.lineWidth = 3.0 

        layer.addSublayer(shapeLayer1)
    }


    func drawPolygon(){
        polygon.move(to: CGPoint(x: 50, y: 50))
        polygon.addLine(to: CGPoint(x: 230, y: 90))
        polygon.addLine(to: CGPoint(x: 240, y: 250))
        polygon.addLine(to: CGPoint(x: 50, y: 280))
        polygon.addLine(to: CGPoint(x: 100, y: 150))
        polygon.addLine(to: CGPoint(x: 50, y: 50))

        shapeLayer4.path = polygon.cgPath

        shapeLayer2.fillColor = UIColor.clear.cgColor 
        shapeLayer2.strokeColor = UIColor.black.cgColor 
        shapeLayer2.lineWidth = 3.0 

         layer.addSublayer(shapeLayer2)
    }


    @IBAction func clickButtonUndo(_ sender: UIButton) {

    }

    @IBAction func clickButtonCircle(_ sender: UIButton) {
        drawCircle()
    }

    @IBAction func clickButtonPolygon(_ sender: UIButton) {
        drawPolygon()
    }




    @IBAction func buttonPurple(_ sender: UIButton) {
        shapeLayer1.path = circle.cgPath
        shapeLayer2.path = polygon.cgPath

        shapeLayer1.fillColor = UIColor.purple.cgColor
        shapeLayer1.strokeColor = UIColor.black.cgColor 
        shapeLayer1.lineWidth = 3.0 

        shapeLayer2.fillColor = UIColor.purple.cgColor 
        shapeLayer2.strokeColor = UIColor.black.cgColor
        shapeLayer2.lineWidth = 3.0 

        layer.addSublayer(shapeLayer1)
        layer.addSublayer(shapeLayer2)

    }

}

您正在自行添加两层

layer.addSublayer(shapeLayer1)
layer.addSublayer(shapeLayer2)

如果你只想拥有一个形状....只使用一个 shapeLayer...在每次点击按钮时为其分配形状...然后调用 setNeedsLayout()..不要使用多个 shapeLayer多个形状

你可以有一个枚举

public enum ShapeType {
         case circle
         case square
         case star
         case Polygon
     }

然后在您的视图中 class

public var shape: ShapeType {
        get {
            return _shape
        }
        set(newValue) {
            _shape = newValue
            setNeedsDisplay()
        }
    }

您可以使用默认形状

    private let ilayer = CAShapeLayer()
    private var _shape: ShapeType = .circle

那你可以写一个函数

// MARK: - Base shape BezierPath

private func getBaseShape(frame:CGRect) -> UIBezierPath? {

    switch _shape {
    case .circle:
        return UIBezierPath(ovalIn: group)
    case .square:
        return  UIBezierPath(roundedRect: group, cornerRadius: 8)
    case .star:
        return drawStarShape()
    case .polygon:
        return drawpolygonPath()

    }
}

并将其分配给形状

let getShapePath = getBaseShape(group: group)

你可以用true/false做一个算法 例如:

var circleBool = true

var polygonBool = true

@IBAction func clickButtonCircle(_ sender: UIButton) {
  if circleBool == true {
    drawCircle()
  polygonBool = false

} }

@IBAction func clickButtonPolygon(_ sender: UIButton) {
   if polygonBool == true {
    drawPolygon()
   circleBool = false

}

@IBAction func clickButtonUndo(_ sender: UIButton) {
circleBool = true
polygonBool = true

}

(撤消按钮 idk 如何清除但尝试绘制 x:0 和 y:0)