如何添加更多块

时间:2015-04-25 17:17:17

标签: xcode swift

我尝试添加var square以及我为squareView所做的所有事情。但我得到的只是一个盒子掉落而另一个只是站在那里。是否有更简单的方法来添加另一个与squareView执行相同操作的框?

import UIKit

class interestViewController: UIViewController {
    var squareView: UIView!
    var square: UIView!
    var gravity: UIGravityBehavior!
    var animator: UIDynamicAnimator!
    var collision: UICollisionBehavior!
    override func viewDidLoad() {
        super.viewDidLoad()
        squareView = UIView(frame: CGRectMake(100, 100, 100, 100))
        square = UIView(frame: CGRectMake(200, 100, 100, 100))

        squareView.backgroundColor = UIColor.blackColor()
        square.backgroundColor = UIColor.blackColor()
        view.addSubview(square)
        view.addSubview(squareView)
        animator = UIDynamicAnimator(referenceView: view)
        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [squareView])
        gravity = UIGravityBehavior(items: [square])
        animator.addBehavior(gravity)
        animator.addBehavior(gravity)
        collision = UICollisionBehavior(items: [squareView])
        collision = UICollisionBehavior(items: [square])
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(collision)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

1 个答案:

答案 0 :(得分:0)

这段代码完全是胡说八道:

gravity = UIGravityBehavior(items: [squareView])
gravity = UIGravityBehavior(items: [square])
animator.addBehavior(gravity)
animator.addBehavior(gravity)

当您将变量gravity设置为一个行为并将其设置为行中的另一个行为时,第一个行为就会被抛出,就好像它从未存在过一样。当你开始添加行为时,自然只会添加一个行为:剩下的一个gravity(然后,你会更加无意义地添加两次)。所以很自然只有一个块是动画的。

以这种方式更简单地思考:

var x = 0
x = 1
x = 2

现在,变量x的价值是多少?如果您认为它是0 1 2,那么您还不了解变量是什么。