I'm required to make a Breakout app in a Single-View Application in the Swift language. However, I'm having trouble getting the "ball" to respond to hitting the barrier. Additionally, I'm having trouble getting the barrier to disappear after it is hit. Does anyone have the solution to this, or does anyone have an example app I can look off of? This is in Single View Application, not 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)
}