重力似乎没有做任何事情,只是无法弄清楚如何让它发挥作用。这是我的代码,红色框不会移动。任何帮助将不胜感激导入 UIKit
class ViewController: UIViewController {
var box : UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
addBox(CGRectMake(100, 100, 30, 30))
createAnimation()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addBox(location: CGRect) {
let newBox = UIView(frame: location)
newBox.backgroundColor = UIColor.redColor()
view.insertSubview(newBox, atIndex: 0)
box = newBox
}
func createAnimation(){
var animate = UIDynamicAnimator(referenceView: self.view)
println("animation")
var gravity = UIGravityBehavior(items: [box])
gravity.gravityDirection = CGVectorMake(0, 0.6)
animate.addBehavior(gravity)
}
}