import UIKit
class ViewController: UIViewController {
@IBOutlet weak var dynamicImageView: UIImageView!
var dynamicAnimator = UIDynamicAnimator()
override func viewDidLoad() {
super.viewDidLoad()
let img1 = UIImage(named:"littleYellowMan.jpg")
dynamicImageView.image = img1
dynamicAnimator = UIDynamicAnimator(referenceView: self.view)
let gravityBehavior = UIGravityBehavior(items: [self.dynamicImageView])
dynamicAnimator.addBehavior(gravityBehavior)
let collisionBehavior = UICollisionBehavior(items: [self.dynamicImageView])
collisionBehavior.translatesReferenceBoundsIntoBoundary = true
dynamicAnimator.addBehavior(collisionBehavior)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
运行结果:图片掉落下去,撞到屏幕的边界