【问题标题】:Adding a collision to a UIView with a pangesture with Swift使用 Swift 向 UIView 添加碰撞
【发布时间】:2015-09-19 20:27:41
【问题描述】:

我正在尝试在不使用 Spritekit 的情况下在 Xcode 中制作乒乓球游戏。我正在为球和桨使用 UIVeiws。为了移动桨,我在情节提要中使用了平移手势识别器并将其连接到 UIView。

我想为桨添加一个碰撞,但我需要它与桨一起移动。现在,使用 addBoundaryWithIdentifier,它仅在桨位于原点时才有效。

求助!到目前为止,这是我的代码...

类 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.
}

}

【问题讨论】:

    标签: swift uipangesturerecognizer uidynamicanimator uicollisionbehavior


    【解决方案1】:

    我通过使桨非常非常重来抵消与球的碰撞来处理这种情况。这可能不是最漂亮的解决方案,但它确实有效!

    @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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多