[英]Adding a collision to a UIView with a pangesture with Swift
我試圖在不使用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.
}
}
我通過使槳非常重以抵消與球的碰撞來處理這種情況。 這可能不是最漂亮的解決方案,但它可以工作!
@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.