简体   繁体   English

使用Swift向手势添加UIView碰撞

[英]Adding a collision to a UIView with a pangesture with Swift

I'm trying to make a pong game in Xcode without using the Spritekit. 我试图在不使用Spritekit的情况下用Xcode制作乒乓游戏。 I'm using UIVeiws for the ball and paddles. 我正在为球和桨使用UIVeiws。 To move the paddle I used a pan gesture recognizer in the storyboard and connected it to the UIView. 为了移动桨,我在情节提要中使用了平移手势识别器,并将其连接到UIView。

I want to add a collision to the paddle, but I need it to move with the paddle. 我想向桨添加碰撞,但是我需要它与桨一起移动。 Right now, with the addBoundaryWithIdentifier, it only works when the paddle is at origin. 现在,使用addBoundaryWithIdentifier,仅当桨叶在原点时才起作用。

Help help! 帮帮忙! Here's my code so far... 到目前为止,这是我的代码...

class ViewController: UIViewController, UICollisionBehaviorDelegate { ViewController类:UIViewController,UICollisionBehaviorDelegate {

var animator = UIDynamicAnimator()
var collision: UICollisionBehavior!
var paddleOneOriginalCenter: CGPoint!

@IBOutlet weak var ball: UIImageView!
@IBOutlet weak var paddleOne: UIView!

@IBAction func panGesturePaddleOne(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(view)
    if sender.state == UIGestureRecognizerState.Began {
        paddleOneOriginalCenter = paddleOne.center
    }
    paddleOne.center.y = paddleOneOriginalCenter.y + translation.y
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Set up ball Dynamic Behaviors
    self.animator = UIDynamicAnimator(referenceView: self.view)

    // Ball Collisions
    collision = UICollisionBehavior(items: [ball])
    collision.addBoundaryWithIdentifier("paddleOne", forPath: UIBezierPath(rect: paddleOne.frame))

    collision.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsets(top: 10, left: 1000, bottom: 10, right: 1000))
    animator.addBehavior(collision)

    // Ball Initial Velocity (Puhs)
    var pushBehavior: UIPushBehavior = UIPushBehavior(items:[ball], mode: UIPushBehaviorMode.Instantaneous)
    pushBehavior.pushDirection = getRandomDirection()
    pushBehavior.magnitude = 1
    animator.addBehavior(pushBehavior)

    // Ball Bounce
    let bounceBehaviour = UIDynamicItemBehavior(items: [ball])
    bounceBehaviour.elasticity = 1.1
    animator.addBehavior(bounceBehaviour)
}

func getRandomDirection() -> CGVector {
    let x = CGFloat(arc4random())
    let y = CGFloat(arc4random())
    return CGVectorMake(x, y)
}

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

} }

I handled the situation by making the paddle very, very heavy to offset the collision with the ball. 我通过使桨非常重以抵消与球的碰撞来处理这种情况。 It's probably not the prettiest solution, but it works! 这可能不是最漂亮的解决方案,但它可以工作!

@IBAction func panGesturePaddleOne(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(view)
    if sender.state == UIGestureRecognizerState.Began {
        paddleOneOriginalCenter = paddleOne.center
    }
    paddleOne.center.y = paddleOneOriginalCenter.y + translation.y
    self.animator.updateItemUsingCurrentState(paddleOne)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Set up ball Dynamic Behaviors
    self.animator = UIDynamicAnimator(referenceView: self.view)


    //Make paddle heavy
    let paddleDynamicProperties = UIDynamicItemBehavior(items: [paddleOne, paddleTwo])
    paddleDynamicProperties.density = 10000000
    paddleDynamicProperties.allowsRotation = false
    animator.addBehavior(paddleDynamicProperties)

    // Ball Collisions
    collision = UICollisionBehavior(items: [ball, paddleOne, paddleTwo])
    collision.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsets(top: 10, left: 1000, bottom: 10, right: 1000))
    animator.addBehavior(collision)
    self.collision.addBoundaryWithIdentifier("left", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(0, self.view.frame.size.height))
     self.collision.addBoundaryWithIdentifier("right", fromPoint: CGPointMake(self.view.frame.size.width, 0), toPoint: CGPointMake(self.view.frame.size.width, self.view.frame.size.height))

    collision.collisionDelegate = self

    // Ball Initial Velocity (Push)
    pushBall()

    // Ball Bounce
    let bounceBehaviour = UIDynamicItemBehavior(items: [ball])
    bounceBehaviour.elasticity = 1.0
    bounceBehaviour.friction = 0
    bounceBehaviour.resistance = 0
    animator.addBehavior(bounceBehaviour)
}

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

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