我试图在iOS swift中做一些动画而不能让重力工作,这里是我的代码,红色框只是一直在移动

时间:2015-01-14 17:48:03

标签: swift uidynamicanimator uigravitybehavior

重力似乎没有做任何事情,只是无法弄清楚如何让它发挥作用。这是我的代码红色框只是停留在移动。任何帮助将不胜感激     导入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)

    }
}

1 个答案:

答案 0 :(得分:0)

问题在于,您的UIDynamicAnimator animate被声明为局部变量。因此它再次出现并且再次出现,没有时间制作任何动画。将其声明为实例变量。