繁体   English   中英

使用UICollisionBehavior调整UIView的大小

[英]Resize UIView with UICollisionBehavior

我最近正在尝试使用UIKitDynamics创建一个类似pong的游戏。 我成功构建了核心游戏。 现在,我想添加一些其他机制。

例如,减少桨叶尺寸的行为。 我添加了一个执行此类操作的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)

}

这是桨控制的功能:

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

}

有没有人意识到这一点?

非常感谢你!

首先向划桨添加边界。 就像是:

yourBehavior.collider.addBoundaryWithIdentifier("aBarrierName", forPath: UIBezierPath(rect: yourPlayerPaddle.frame))

然后使用func crashBehavior检查冲突并执行转换。 就像是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM