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

问题描述 投票:0回答:2

我是新手,很快,现在我正在与简单的事情作斗争。

如果我单击撤消按钮,则MyDrawView应该清晰,如果我单击多边形(例如紫色),然后单击圆形,则只有一个对象。

类似这样:

only 1 object

但是现在我有这个:

but now i have this

我的代码:

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)

    }

}
ios swift xcode drawing cashapelayer
2个回答
0
投票

您将自己添加两层

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

[如果您只想拥有一个形状...。仅使用一个shapeLayer ...在每次单击按钮时为其分配形状...并调用setNeedsLayout() ..请勿将多个shapeLayers用于多个形状

您可以有一个枚举

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

然后在您的View类中

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)

0
投票

您可以将算法设置为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)

© www.soinside.com 2019 - 2024. All rights reserved.