i am beginner in swift, and now i am a little fighting with maybe simple thing.
if i click on button undo, the MyDrawView should be clear, and if i click on polygon a fill for example purple, and then i click on circle, to have only one object.
something like this:
but now i have this:
my code:
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)
}
}
you can make an algorithm with true/false for example:
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
}
(for undo button idk how to clear but try to draw x:0 and y:0)