-1

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

    }
}
4

1 回答 1

0

问题是animate,您的 UIDynamicAnimator 被声明为局部变量。因此,它开始存在并再次退出存在,没有时间为任何东西设置动画。而是将其声明为实例变量。

于 2015-01-14T17:49:41.210 回答