title: Stop UIView from going off screen
body: <p>I have a UIView that is a circle. That is all the app is - I can move the circle around the screen using a <code>UIPanGestureRecognizer.</code> Now I don't want my circle to be able to be dragged off screen. For example, if I drag the circle to the right, it should stop moving the circle when the right edge hits the edge of the window.</p>
  
  <p>Here is my code:</p>
  
  <pre><code> switch rec.state {
          case .Began:
              x = fingerLocation.x - (myView?.center.x)!
              y = fingerLocation.y - (myView?.center.y)!
              break
  
  
          case .Changed:
              myView?.center.x = fingerLocation.x - x
              myView?.center.y = fingerLocation.y - y
  
              if (myView?.center.x)! + (myView!.bounds.width/2) &gt;= view.bounds.width {
                  myView?.center.x = view.bounds.width - myView!.bounds.width/2
              }
              break
          case .Ended:
              myView?.center = CGPointMake(fingerLocation.x - x, fingerLocation.y - y)
              break
  }
  </code></pre>
  
  <p>This code works if I drag the circle slowly toward the edge. If I drag quickly, the circle will go over the edge, and jump back into view the second another <code>.Changed</code> state is sent. <strong>How can I stop the circle from ever going over the edge?</strong></p>