# How to clear uiview drawing

**URL:** <https://forums.kodeco.com/t/how-to-clear-uiview-drawing/38576>\
**Category:** Swift\
**Created:** [January 15, 2018, 3:30am UTC](https://forums.kodeco.com/t/how-to-clear-uiview-drawing/38576 "2018-01-15T03:30:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [January 15, 2018, 3:30am UTC](https://forums.kodeco.com/t/how-to-clear-uiview-drawing/38576/1 "2018-01-15T03:30:36Z")

</div>

I am working on a drawing app where the user draws something on a view. On the same view controller I would like to create a button that would erase everything that was on the view and give it a blank slate. How would I do this? I added all of my code that is attached to the uiview to draw the object.

```
           @IBOutlet weak var jj: draw!

           @IBAction func enterScore(_ sender: Any) {}

```

Code for drawing pen below

```
                   import UIKit
     struct stroke {
let startPoint: CGPoint
let endPoint: CGPoint
let color: CGColor

    }
     class drawViewSwift: subClassOFUIVIEW {

var isDrawing = false
var lastPoint : CGPoint!
var strokeColor : CGColor = UIColor.black.cgColor
var storkes = [stroke]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard !isDrawing else {return}
    isDrawing = true
    guard let touch = touches.first else {return}
    let currentPoint = touch.location(in: self)

    lastPoint = currentPoint
    setNeedsDisplay()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard isDrawing else {return}
    guard let touch = touches.first else {return}
    setNeedsDisplay()

    let currentPoint = touch.location(in: self)
    print(currentPoint)
    let sstroke = stroke(startPoint: lastPoint, endPoint: currentPoint, color: strokeColor)
    storkes.append(sstroke)

    lastPoint = currentPoint

}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard isDrawing else {return}
    isDrawing = false
    guard let touch = touches.first else {return}
    let currentPoint = touch.location(in: self)
    let sstroke = stroke(startPoint: lastPoint, endPoint: currentPoint, color: strokeColor)
    storkes.append(sstroke)
    lastPoint = nil
    setNeedsDisplay()
    print(currentPoint)
}

override func draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    context?.setLineWidth(10)
    context?.setLineCap(.round)
    for stroke in storkes {
        context?.beginPath()
        context?.move(to:stroke.startPoint)
        context?.addLine(to: stroke.endPoint)
        context?.setStrokeColor(stroke.color)
        context?.strokePath()
    }
    setNeedsDisplay()

}

func crase() {
    storkes = []
    strokeColor = UIColor.black.cgColor
    setNeedsDisplay()
}
    }
     class subClassOFUIVIEW: UIView {

override func awakeFromNib() {

    layer.shadowOpacity = 1
    layer.shadowRadius = 1
    layer.shadowOffset = CGSize(width: 1, height: 1)
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowPath = CGPath(rect: bounds, transform: nil)
    layer.shadowPath = CGPath(rect: bounds, transform: nil)
}}

```

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [January 16, 2018, 12:10am UTC](https://forums.kodeco.com/t/how-to-clear-uiview-drawing/38576/2 "2018-01-16T00:10:10Z")

</div>

Hi Tim,  
you already have the infrastructure to support that. You are saving each stroke in an array and then when you call the `setNeedsDisplay` it draws all of the strokes. Now to delete everything, empty out your arrays and there will be nothing to draw.

However you might need to clear the context using the `context?.clear(rect)`

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [June 30, 2018, 7:43pm UTC](https://forums.kodeco.com/t/how-to-clear-uiview-drawing/38576/3 "2018-06-30T19:43:52Z")

</div>

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