title: Redrawing CGContext after it's already been drawn in Swift?
body: <p>I am trying to make a simple single view app for my own practice, but I ran into a problem and haven't got around this problem for past couple of days. </p>
  
  <p><a href="https://i.stack.imgur.com/qGe0c.png" rel="nofollow noreferrer">Screenshot of the current program</a></p>
  
  <p>Basically, when I press any of these style buttons or adjust the slider, I want it to redraw the CGContext that's on the bottom of the screen (red dotted line). For example, if I press the STYLE 1 button, it should call the function called drawStyleOne() and apply these two different changes:  </p>
  
  <pre><code>context?.setLineDash(phase: 0, lengths: [2,3])
  context?.setStrokeColor(UIColor.green.cgColor)
  </code></pre>
  
  <p>My goal is to re-draw the dotted line (smaller dot) and change the color of the context to green. (context is an instance of CGContext) </p>
  
  <p>I have everything drawn on the override func draw(_ rect: CGRect), but I cannot get it to re-draw the CGContext to the way I want it to.</p>
  
  <p>Here's part of my code to help explain this situation bit better.</p>
  
  <pre><code>import UIKit
  
  class VectorView: UIView {
  private var context: CGContext? = nil
  
  override init(frame: CGRect) {
      super.init(frame: frame)
      //contentMode = .redraw //tried this, didn't work.
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  override func draw(_ rect: CGRect) {
      // lets us make calls
      context = UIGraphicsGetCurrentContext()!
      context?.move(to: CGPoint(x: 0.0, y: 10.0))
      context?.setLineDash(phase: 0, lengths: [2,3])
      context?.setLineJoin(CGLineJoin.bevel)
      context?.setLineCap(CGLineCap.square)
  
      context?.addLine(to: CGPoint(x: 50.0, y: 70.0))
      context?.addLine(to: CGPoint(x: 100.0, y: 20.0))
  
      context?.setLineWidth(1.0)
      context?.setStrokeColor(UIColor.red.cgColor)
      context?.drawPath(using: CGPathDrawingMode.stroke)        
  }
  
  func drawStyleOne () {
  
      context = UIGraphicsGetCurrentContext()
      context?.move(to: CGPoint(x: 0.0, y: 10.0))
      context?.setLineDash(phase: 0, lengths: [2,3])
      context?.setLineJoin(CGLineJoin.bevel)
      context?.setLineCap(CGLineCap.square)
  
      context?.addLine(to: CGPoint(x: 50.0, y: 70.0))
      context?.addLine(to: CGPoint(x: 100.0, y: 20.0))
  
      context?.setStrokeColor(UIColor.green.cgColor)
  
      context?.drawPath(using: CGPathDrawingMode.stroke)
      self.setNeedsDisplay()
      //contentMode = .redraw //Tried this and didn't work either..
      NSLog("drawStyleOne got called")
  }
  </code></pre>
  
  <p>and the drawStyleOne function from class above gets called inside the AppDelegate.</p>
  
  <pre><code>import UIKit
  
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
  
      var window: UIWindow?
      let screenSize = UIScreen.main.bounds
  
      private var _vectorView: VectorView? = nil
      private var _buttonStylesView: ButtonStyleView? = nil
  
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
  
          window = UIWindow()
          window?.rootViewController = ViewController()
          window?.rootViewController?.view.backgroundColor = UIColor.white
          window?.makeKeyAndVisible()
  
          _vectorView = VectorView()
          _vectorView?.frame = CGRect(x: 0.0, y: 650, width: 414, height: 70)
          _vectorView?.backgroundColor = UIColor.lightGray
          window?.rootViewController?.view.addSubview(_vectorView!)
  
          _buttonStylesView = ButtonStyleView()
          _buttonStylesView?.frame = CGRect (x: screenSize.width / 10, y: screenSize.height * 6 / 10, width: 414, height: 100)
          _buttonStylesView?.backgroundColor = UIColor.clear
  
          window?.rootViewController?.view.addSubview(_buttonStylesView!)
  
          _buttonStylesView?.styleOne?.addTarget(self, action: #selector(styleButtonPressed), for: UIControlEvents.touchDown)     
          return true
      }
  
      func styleButtonPressed() {
          NSLog("STYLE BUTTON PRESSED")
          _vectorView?.drawStyleOne()
      }
  }
  </code></pre>
  
  <p>I even put NSLog("drawStyleOne got called") at the end just to verify that the function is getting called (and it does), however it will not redraw the line no matter what I try. </p>
  
  <p>Any help/advice would be much appreciated.</p>