ios - 使用 UICollisionBehavior 调整 UIView 的大小

标签 ios iphone swift pong uikit-dynamics

我最近尝试使用 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/

相关文章:

iphone - 将 JSON 文件发送到 iPhone 以进行 iOS 应用程序测试

iPhone "Web Site Error"

iphone - objective-c : for-loop gets slower and slower

ios - Swift 如何在 UITableView 中为多个部分设置缓存标签值

ios - UITextField 输入不工作

ios - 如何在 Objective c 中隐藏谷歌广告

ios - 我无法在标签上打印我的计算结果

ios - Xcode:从父项目继承 `configuration` s - 不可能吗?

ios - 如何在 Swift 中为不同文件中的类定义函数?

ios - 使用符合被覆盖 var 协议(protocol)子协议(protocol)的 var 覆盖符合协议(protocol)的 var