我最近正在尝试用UIKitDynamics创建一个类似乒乓球的小游戏。我成功地构建了核心游戏。现在我想添加一些额外的机制。
例如,一种行为,它减小了桨的大小。我已经添加了一个可以执行这种操作的NSTimer。但我注意到,划板的CollisionBehavior不会与划板同时调整大小。
下面是我的代码:
func decreaseBarSize() {
player1Bar.bounds = CGRect(x: player1Bar.frame.minX, y: player1Bar.frame.minY, width: player1Bar.bounds.width - 1, height: player1Bar.frame.height)
player1Bar.layoutIfNeeded()
animator.updateItemUsingCurrentState(player1Bar)
}下面是paddle-control的功能:
func moveBar() {
if player == .Player1 {
let barFrame = player1Bar.frame
switch self.direction {
case .Right:
if Int(barFrame.maxX) < superviewWidth - 10 {
player1Bar.frame = CGRect(x: barFrame.minX + 1, y: barFrame.minY, width: barFrame.width, height: barFrame.height)
}
break
case .Left:
if Int(barFrame.minX) > 10 {
player1Bar.frame = CGRect(x: player1Bar.frame.minX - 1, y: barFrame.minY, width: barFrame.width, height: barFrame.height)
}
break
default:
break
}
animator.updateItemUsingCurrentState(player1Bar)
} else if player == .Player2 {
let barFrame = player2Bar.frame
switch self.direction {
case .Right:
if Int(barFrame.maxX) < superviewWidth - 10 {
player2Bar.frame = CGRect(x: barFrame.minX + 1, y: barFrame.minY, width: barFrame.width, height: barFrame.height)
}
break
case .Left:
if Int(barFrame.minX) > 10 {
player2Bar.frame = CGRect(x: barFrame.minX - 1, y: barFrame.minY, width: barFrame.width, height: barFrame.height)
}
break
default:
break
}
animator.updateItemUsingCurrentState(player2Bar)
}
}有没有人能意识到这一点?
非常感谢!
发布于 2016-04-10 20:45:30
首先,给球拍添加一个边界。类似于:
yourBehavior.collider.addBoundaryWithIdentifier("aBarrierName", forPath: UIBezierPath(rect: yourPlayerPaddle.frame))然后使用func collisionBehavior检查冲突并执行转换。类似于:
func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, atPoint p: CGPoint) {
print("Contact by - \(identifier)")
let collidingView = item as? UIView
collidingView?.transform = CGAffineTransformMakeScale(1.5 , 1)
UIView.animateWithDuration(0.4) {
collidingView?.transform = CGAffineTransformMakeScale(1 , 1)
}
}https://stackoverflow.com/questions/34131638
复制相似问题