简体   繁体   English

Swift中的UICollisions

[英]UICollisions in Swift

I'm required to make a Breakout app in a Single-View Application in the Swift language. 我需要使用Swift语言在单视图应用程序中制作Breakout应用程序。 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. 这是在单视图应用程序中,而不是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)    
}

You need to have both the objects in your UICollisionBehavior if you want them to collide. 如果要使两个对象碰撞,则需要在UICollisionBehavior都包含两个对象。 Depending on what you need there is several possibilities. 根据您的需要,有几种可能性。 I suppose that you need your ball to bounce on barrier, so create the behavior: 我想您需要球在障碍物上反弹,因此请创建以下行为:

let collisionBehavior = UICollisionBehavior(blueSquare: array)

and then add a rigid boundary that correspond to your barrier: 然后添加与您的障碍相对应的刚性边界:

let edge = CGPointMake(barrier.frame.origin.x + barrier.frame.size.width,
                       barrier.frame.origin.y + barrier.frame.size.heigth);
collisionBehavior.addBoundaryWithIdentifier("barrier",
                                            fromPoint:barrier.frame.origin,
                                            toPoint:rightEdge];

Now if you want to capture the collision you need to add a delegate to collisionDelegate . 现在,如果您想捕获碰撞,则需要将一个委托添加到collisionDelegate This delegate should be able to respond to several methods when a hit occurs, (read documentation about UICollisionBehaviorDelegate . 发生点击时,该委托人应该能够响应多种方法(请阅读有关UICollisionBehaviorDelegate的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM