【发布时间】:2017-03-29 16:19:54
【问题描述】:
我的目标是让一组具有CGRect 属性的UIViews 从屏幕外掉下来然后堆积起来。现在,我有一个圈子在工作,但每次我尝试添加另一个圈子时,它们就不再有动画了。我正在使用UIDynamics 及其gravity 函数。这是我的代码:
func addBox(location: CGRect) -> UIView {
let newBox = UIView(frame: location)
newBox.backgroundColor = UIColor(red: 186.0/255.0, green: 216.0/255.0, blue: 242.0/255.0, alpha: 1.0)
newBox.layer.cornerRadius = newBox.frame.width/2
hView.insertSubview(newBox, at: 0)
return newBox
}
addBox(location: CGRect(x: 100, y: -100, width: 100, height: 100))
var animator: UIDynamicAnimator? = nil;
let gravity = UIGravityBehavior()
var collision: UICollisionBehavior!
animator = UIDynamicAnimator(referenceView: hView)
func createAnimatorStuff(box: UIView) {
collision = UICollisionBehavior(items: [box])
collision.translatesReferenceBoundsIntoBoundary = true
animator?.addBehavior(collision)
gravity.addItem(box)
gravity.gravityDirection = CGVector(dx: 0, dy: 0.8)
animator?.addBehavior(gravity)
}
func dropDown() {
let xVal = 100
let xVal2 = 700
let box = addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
let box2 = addBox(location: CGRect(x: xVal2, y: 100, width: 100, height: 100))
createAnimatorStuff(box: box)
createAnimatorStuff(box: box2)
}
dropDown()
如果有人可以帮助我并调整我的代码以创建更多的圆圈下降然后堆积,我将非常感激。真的。请提出任何问题,并在此先感谢您。
【问题讨论】:
标签: ios swift uikit uikit-dynamics