How to create a color to erase what you have drawn on a uiview [duplicate]

My code below is in 2 seperate classes. From class gooGoo I want to be able to call func change to be used as a eraser to what was drawn on the uiview. If I change drawPlace.drawColor = UIColor.black to drawPlace.drawColor = UIColor.clear it does nothing. That is what I was thinking of doing.

                         class gooGoo : UIViewController{
var myLayer = CALayer()
var path = UIBezierPath()
var startPoint = CGPoint()
var touchPoint = CGPoint()
var drawPlace = DrawingView()

    func Change(){
        drawPlace.drawColor = UIColor.black
    }

              }
               class DrawingView: UIView {

               var drawColor = UIColor.black
             var lineWidth: CGFloat = 5

           private var lastPoint: CGPoint!
          private var bezierPath: UIBezierPath!
         private var pointCounter: Int = 0
      private let pointLimit: Int = 128
      private var preRenderImage: UIImage!

       // MARK: - Initialization

      override init(frame: CGRect) {
super.init(frame: frame)

initBezierPath()


              }





           required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)

        initBezierPath()
          }

                     func initBezierPath() {
bezierPath = UIBezierPath()
bezierPath.lineCapStyle = CGLineCap.round
bezierPath.lineJoinStyle = CGLineJoin.round
             }

               // MARK: - Touch handling

               override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
             let touch: AnyObject? = touches.first
     lastPoint = touch!.location(in: self)
      pointCounter = 0
           }

                         override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch: AnyObject? = touches.first
             let newPoint = touch!.location(in: self)

            bezierPath.move(to: lastPoint)
         bezierPath.addLine(to: newPoint)
             lastPoint = newPoint

               pointCounter += 1

              if pointCounter == pointLimit {
    pointCounter = 0
    renderToImage()
    setNeedsDisplay()
    bezierPath.removeAllPoints()
}
else {
    setNeedsDisplay()
}
}

                 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
pointCounter = 0
renderToImage()
setNeedsDisplay()
bezierPath.removeAllPoints()
           }

               override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
touchesEnded(touches!, with: event)
                   }

            // MARK: - Pre render

            func renderToImage() {

UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
if preRenderImage != nil {
    preRenderImage.draw(in: self.bounds)
}

bezierPath.lineWidth = lineWidth
drawColor.setFill()
drawColor.setStroke()

bezierPath.stroke()

preRenderImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
              }

                // MARK: - Render

                     override func draw(_ rect: CGRect) {
             super.draw(rect)

if preRenderImage != nil {
    preRenderImage.draw(in: self.bounds)
    }

bezierPath.lineWidth = lineWidth
drawColor.setFill()
drawColor.setStroke()
bezierPath.stroke()
               }

             // MARK: - Clearing

            func clear() {
preRenderImage = nil
bezierPath.removeAllPoints()
setNeedsDisplay()
                    }

             // MARK: - Other

           func hasLines() -> Bool {
return preRenderImage != nil || !bezierPath.isEmpty
              }}

@timswift Do you still have issues with this?

Hi @timswift,
I am not sure about your question, but if you want to erase then clear colour is not what you are after, .clear is transparent. to erase you need to use the background colour.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.