resize UIDynamicItem

I'm trying to use UIKit Dynamics together with animations. The object (in my case just a simple square view) is resized, but the Collision-boundaries are not refreshed to reflect the new size of the square.


With the following simple ViewController my problem can be reconstructed:


class UIDynamicsTestViewController: UIViewController {

   var animator: UIDynamicAnimator!

   var collision: UICollisionBehavior!

   var gravity: UIGravityBehavior!


   override func viewDidLoad() {

      super.viewDidLoad()

      let square = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))

      square.backgroundColor = UIColor.blueColor()

      view.addSubview(square)

     

      animator = UIDynamicAnimator(referenceView: self.view)

      collision = UICollisionBehavior(items: [square])

      collision.translatesReferenceBoundsIntoBoundary = true

      animator.addBehavior(collision)

      gravity = UIGravityBehavior(items: [square])

      animator.addBehavior(gravity)

      UIView.animateWithDuration(1.0, animations: {

         square.frame.size = CGSize(width: 50,height: 50)

      })

   }

}


This example makes the square fall down thru gravity and bounce a little bit on the bottom border of the screen. At the same time the square shrinks.


The Problem is that the square will not 'land' directly on the bottom border but float approximately 100 pixels over the border as if it didn't resize

How can I refresh the boundaries used for collision?

ideally you would need to call animator.updateItem(usingCurrentState: square). But this is not going to work and looks like a bug in Apple's code.
To workaround this issue you would need to remove all the behaviours which contain your square, call update item and then add those behaviours back.
Code Block
animator.removeBehavior(collision)
animator.removeBehavior(gravity)
animator.updateItem(usingCurrentState: square)
animator.addBehavior(collision)
animator.addBehavior(gravity)

resize UIDynamicItem
 
 
Q