1 import UIKit 2 3 class ViewController: UIViewController { 4 5 @IBOutlet weak var dview: UIView! 6 var dynamicAnimator = UIDynamicAnimator(); 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view, typically from a nib 10 dynamicAnimator = UIDynamicAnimator(referenceView: self.view); 11 // 創建并添加重力行為 12 let gravityBehavior = UIGravityBehavior(items: [self.dview]); 13 dynamicAnimator.addBehavior(gravityBehavior); 14 15 // 創建并添加碰撞行為 16 let collisionBehavior = UICollisionBehavior(items: [self.dview]); 17 collisionBehavior.translatesReferenceBoundsIntoBoundary = true; 18 dynamicAnimator.addBehavior(collisionBehavior); 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 // Dispose of any resources that can be recreated. 24 } 25 override func viewDidAppear(animated: Bool) { 26 super.viewDidAppear(animated); 27 28 } 29 30 }
参考资料:
抄自《Swift与Cocoa框架开发》