0

I have made a greenView:

static func createRectView(
    x: Float,
    y: Float,
    width: Float,
    height: Float,
    color: UIColor,
    transparency: Float
) -> UIView {
    let viewRectFrame = CGRect(
        x: CGFloat(x),
        y: CGFloat(y),
        width: CGFloat(width),
        height: CGFloat(height)
    )
    let retView = UIView(frame:viewRectFrame)
    retView.backgroundColor = color
    retView.alpha = CGFloat(transparency)
    return retView
}
let greenView: UIView = createRectView(
    x: 50,
    y: 110,
    width: 150,
    height: 100,
    color: .green,
    transparency: 1
)
view.addSubview(greenView)

I have drawn a line on greenView:

func getPath() -> UIBezierPath {
    let path = UIBezierPath()
    path.move(to: CGPoint(x: 0, y: 0))
    path.addLine(to: pointDef5)
    return path
}

func createLine(on view: UIView) {
    let shapeLayer = CAShapeLayer()
    view.layer.addSublayer(shapeLayer)
    shapeLayer.strokeColor = UIColor.black.cgColor
    shapeLayer.lineWidth = 5
    shapeLayer.fillColor = nil 
    shapeLayer.path = getPath().cgPath
}

createLine(on: greenView)

I have removed and added greenView:

greenView.removeFromSuperview()
view.addSubview(greenView)

I see my old line on absurd greenView! How delete my line from my greenView?

greenView + line = forever? I don't know how delete a line

enter image description here

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571

1 Answers1

1

In your code, greenView is created once and then added to self.view. Removing and re-adding this view does not change the view.

The line in your code is added as a sublayer on greenView. To remove the line, you must remove the sublayer.

For example, try this:

greenView.layer.sublayers?.removeAll()
Ely
  • 8,259
  • 1
  • 54
  • 67