title: Change Image Item after Motion Shake and Collision Detected in in UIDynamics Swift
body: <p>I have the following code and I want to show an alert and change the image after the collision and based on a random number generated.
  However, it shows the result before the collision...
  Any idea???</p>
  
  <pre><code>import UIKit
  class ViewController: UIViewController,UICollisionBehaviorDelegate {    
  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var shakelabel: UILabel!
  
  var animator: UIDynamicAnimator!
  var gravity: UIGravityBehavior!
  var collision: UICollisionBehavior!
  
  // Random number generation
  let Bump = Int(arc4random_uniform(UInt32(2)))
  
  override func motionEnded(motion: UIEventSubtype,
      withEvent event: UIEvent?) {
  
        if motion == .MotionShake {
              animator = UIDynamicAnimator(referenceView: view)
              gravity = UIGravityBehavior(items: [imageView])
              animator.addBehavior(gravity)
              collision = UICollisionBehavior(items: [imageView])
              collision.translatesReferenceBoundsIntoBoundary = true
              animator.addBehavior(collision)
          }          
          if Bump == 0 {
              let controller = UIAlertController(title: "W O W",
              message: "YOU WON",
              preferredStyle: .Alert)
              controller.addAction(UIAlertAction(title: "OK",
              style: .Default,
              handler: nil))
              presentViewController(controller, animated: true, completion: nil)             
          }
          if Bump == 1 {
              imageView.image = UIImage(named: "image_loose")               
              let controller = UIAlertController(title: "O O P S",
              message: "YOU LOOSE",
              preferredStyle: .Alert)
              controller.addAction(UIAlertAction(title: "OK",
              style: .Default,
              handler: nil))
              presentViewController(controller, animated: true, completion: nil)
          }
  }
  override func viewDidLoad() {
      super.viewDidLoad()
    }
  }
  </code></pre>