gpt4 book ai didi

ios - 使用 UICollisionBehavior 调整 UIView 的大小

转载 作者:行者123 更新时间:2023-11-28 08:51:24 28 4
gpt4 key购买 nike

我最近尝试使用 UIKitDynamics 创建一个类似乒乓球的小游戏。我成功地构建了核心游戏。现在我想添加一些额外的机制。

例如,一种行为会减小 Racket 的大小。我添加了一个执行此类操作的 NSTimer。但我注意到 Racket 的 CollisionBehavior 不会与 Racket 同时调整大小。

这是我的代码:

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)

}

这是桨式控件的功能:

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

}

有没有人知道如何实现这一点?

非常感谢!

最佳答案

首先给 Racket 添加一个边界。像这样的东西:

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

关于ios - 使用 UICollisionBehavior 调整 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131638/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com