0
我正在嘗試在Xcode中使用Spritekit而不使用Spritekit。我使用UIVeiws作爲球和槳。爲了移動槳,我在故事板中使用了一個平移手勢識別器並將其連接到UIView。使用Swift將衝突添加到具有pangesture的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.
}
}