title: iOS Swift draw to screen
body: <p>I am new to iOS development and need help from someone a bit more experienced than me. I searched the internet and couldn't find any working solution.</p>
  
  <p>I need to draw to the screen like canvas in Android. Currently I have a <code>CADisplayLink</code> to call a function every frame. And that's working well. The problem is: How do I actually draw anything, like a rectangle, a circle or a line to the screen every frame? </p>
  
  <p>This is what I have (I linked this class to the view in the storyboard):</p>
  
  <pre><code>class Canvas: UIView {
      override func draw(_ rect: CGRect) {
          let context = UIGraphicsGetCurrentContext()
          context?.setLineWidth(2.0)
          context?.setStrokeColor(UIColor.green.cgColor)
          context?.move(to: CGPoint(x: 30, y: 30))
          context?.addLine(to: CGPoint(x: Double(xBall), y: Double(yBall)))
          context?.strokePath()
     }
  }
  </code></pre>
  
  <p>With the following code I can actually draw a line to the screen:</p>
  
  <pre><code>let canvas = Canvas()
  canvas.draw(CGRect())
  </code></pre>
  
  <p>The problem is, that this works exactly ONE time. When I have <code>canvas.draw(CGRect())</code> in my loop which repeats every frame, it works for the first frame (the initial values of <code>xBall</code> and <code>yBall</code>) and never again. When I print the values in the <code>draw</code> method, it gets called every frame and the variables have the correct values. But it does not draw it to the screen. I tried adding the line <code>setNeedsDisplay()</code> in the <code>draw</code> method, with similar results.</p>
  
  <p>Any help will be appreciated! Thanks!</p>