title: Swift: UIDynamic animate panel from bottom to top
body: <p>I want to build a simple custom view which slides up from the bottom with UIDynamicAnimator. </p>
  
  <p>When the app loads I offset the view from the screen using:</p>
  
  <pre><code>panelWidth   = originView!.frame.size.width
  panelHeight  = originView!.frame.size.height/2
  screenHeight = originView!.frame.size.height
  
  // Set the frame for the container of this view based on the parent
  container.frame = CGRectMake(0, screenHeight, panelWidth, panelHeight)
  </code></pre>
  
  <p>To envision this, I expect the view to appear as:</p>
  
  <pre><code>|-----|
  |     |
  |  A  | &lt;--- Main View 
  |     | 
  |-----|
  |-----|
  |  B  | &lt;--- Second View (offset by screen height so it draws directly under (0,height))
  |-----|
  </code></pre>
  
  <p>I have set up gesture recognisers to detect when I swipe up and down (up to show, down to dismiss), however right now the view bounces off the screen and I can never seem to get it back.</p>
  
  <p>With my animator, I use the following code</p>
  
  <pre><code>animator.removeAllBehaviors()
  isOpen = open
  
  let gravityY:CGFloat  = (open) ? -0.5 : 0.5 
  let gravityX:CGFloat  = 0  
  let magnitude:CGFloat = (open) ? -20 : 20
  let boundaryY:CGFloat = (open) ? -panelHeight : panelHeight
  let boundaryX:CGFloat = (open) ? 0 : 0
  </code></pre>
  
  <p>Before showing the rest of the code, I think I should discuss my constants. Vector math is not my strong point so this is probably the sole reason all of this is going wrong.  I set the values to be negative on when <code>open</code> is true because I want to animate the view back onto the screen which means animating it upward, therefore I assume I need negative gravity.</p>
  
  <p>I set boundaryY to be the height of the panel as I want to animate it up onto the screen only to the height of the window, currently it flies off the top (when I debugged screen height = 667 and panelHeight = 333.5 so I don't understand why it exceeds that mark)</p>
  
  <pre><code>// animator behaviours - here I just create animaters on the panels container
  let gravityBehaviour:UIGravityBehavior = UIGravityBehavior(items: [container])
  let collisionBehaviour:UICollisionBehavior = UICollisionBehavior(items: [container])
  let pushBehaviour:UIPushBehavior = UIPushBehavior(items: [container], mode: UIPushBehaviorMode.Instantaneous)
  let panelBehaviour:UIDynamicItemBehavior = UIDynamicItemBehavior(items:[container])
  
  // Gravity behaviour - I don't want it to move in X direction, only Y
  gravityBehaviour.gravityDirection = CGVectorMake(gravityX, gravityY)
  
  // collision detection - I think this is the major problem, how does this work...
  collisionBehaviour.addBoundaryWithIdentifier("basePanelBoundary", fromPoint: CGPointMake(boundaryX, 0), toPoint: CGPointMake(boundaryX, boundaryY))
  
  // push behaviours
  pushBehaviour.magnitude = magnitude
  
  // panel behaviours
  panelBehaviour.elasticity = 0.3
  
  // bind behaviours
  animator.addBehavior(gravityBehaviour)
  animator.addBehavior(collisionBehaviour)
  animator.addBehavior(pushBehaviour)
  animator.addBehavior(panelBehaviour)
  </code></pre>
  
  <p>I'm mainly concerned with the collision detection as I believe this is what is causing the view to rotate and fling off the screen, how can I fix this?</p>