我需要在 Swift 语言的单视图应用程序中制作一个 Breakout 应用程序。但是,我无法让“球”对击中障碍做出反应。此外,我无法让障碍物在被击中后消失。有没有人对此有解决方案,或者有没有人有我可以查看的示例应用程序?这是在单视图应用程序中,而不是 Sprite。
var dynamicAnimatior = UIDynamicAnimator()
override func viewDidLoad() {
super.viewDidLoad()
dynamicAnimatior = UIDynamicAnimator(referenceView: view)
setupViews()
}
func setupViews() {
let blueSquare = UIView(frame: CGRectMake(100, 100, 50, 50))
blueSquare.backgroundColor = UIColor.blueColor()
view.addSubview(blueSquare)
let barrier = UIView(frame: CGRect(x: 0, y: 300, width: 130, height: 20))
barrier.backgroundColor = UIColor.redColor()
view.addSubview(barrier)
addDynamicBehaviors([blueSquare])
}
func addDynamicBehaviors(array: [UIView]) {
let dynamicItemBehavior = UIDynamicItemBehavior(items: array)
dynamicItemBehavior.density = 1.0
dynamicItemBehavior.friction = 0.0
dynamicItemBehavior.resistance = 0.0
dynamicItemBehavior.elasticity = 1.0
dynamicAnimatior.addBehavior(dynamicItemBehavior)
let pushBehavior = UIPushBehavior(items: array, mode: .Instantaneous)
pushBehavior.magnitude = 1.0
pushBehavior.pushDirection = CGVectorMake(0.5, 0.5)
dynamicAnimatior.addBehavior(pushBehavior)
let collisionBehavior = UICollisionBehavior(items: array)
collisionBehavior.translatesReferenceBoundsIntoBoundary = true
collisionBehavior.collisionMode = .Everything
collisionBehavior.collisionDelegate = self
dynamicAnimatior.addBehavior(collisionBehavior)
}