import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var dynamicAnimator = UIDynamicAnimator()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
dynamicAnimator =
UIDynamicAnimator(referenceView: self.view)
//創(chuàng)建并添加重力行為
let gravityBehavior = UIGravityBehavior(items: [self.imageView])
dynamicAnimator.addBehavior(gravityBehavior)
//創(chuàng)建并添加碰撞行為let collisionBehavior =
UICollisionBehavior(items: [self.imageView])
collisionBehavior.translatesReferenceBoundsIntoBoundary = true
dynamicAnimator.addBehavior(collisionBehavior)
}
}