swift - CollisionBehavior 未被调用

标签 swift uikit uicollisionbehavior

我一直在尝试让这个 CollisionBehavior 函数运行。

lazy var collisionDelegate: UICollisionBehaviorDelegate = self

func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item1: UIDynamicItem, with item2: UIDynamicItem, at p: CGPoint) {
    print("colliding")
}
func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint) {
    print("colliding")
}

如您所见,我尝试了两种 collisionBehavior 方法。球和 block 都是动态对象,我将屏幕末端作为边界。因此,每次一个 block 或“球”撞击另一个 block 、桨、球或屏幕的末端时,它应该打印“碰撞”,但没有任何内容打印到终端。下面是 block 、球、桨和边界的代码。

桨:

    func paddle () {
    lastPaddle.removeFromSuperview()
    collider.removeItem(lastPaddle)

    let yPos = CGFloat(bounds.size.height / 6 * 5)
    let width = bounds.size.width / 4
    let height = bounds.size.width / 20
    if !first {xPos = bounds.midX; first = true}

    let paddle = CGRect(x: xPos + width/2, y: yPos + height/2, width: width, height: height)
    let frame = UIView(frame: paddle)
    frame.backgroundColor = UIColor.red()
    addSubview(frame)
    let item: UIDynamicItem = frame

    let dib = UIDynamicItemBehavior()
    animator.addBehavior(dib)
    dib.allowsRotation = false
    dib.isAnchored = true
    dib.elasticity = 0

    dib.addItem(item)
    collider.addItem(item)
    lastPaddle = frame


    //collider.removeBoundary(withIdentifier: "paddle")

    //update()
}

球:

func createBall () {


    xBall = bounds.midX
    yBall = bounds.midY

    let smallRect = CGRect(x: xBall, y: yBall, width: bounds.size.width/12, height: bounds.size.width/12)
    //let lBall = CGPath(ellipseIn: smallRect, transform: nil)
    ball = smallRect
    let frame = UIView(frame: smallRect)
    frame.backgroundColor = UIColor.green()
    addSubview(frame)
    let item: UIDynamicItem = frame
    //collider.elasticity = 100
    gravity.magnitude = 0.5
    gravity.addItem(item)
    collider.addItem(item)
    //let arr = [item]

    animator.addBehavior(ballBehaviour)

    ballBehaviour.elasticity = 1.5

    ballBehaviour.addItem(item)


}

block 和屏幕结束(边界):

    func createBlocks () {
    for a in 0..<numberOfRows {
        for b in 0..<numberOfColumns {
            //let view = UIView()
            let x = CGFloat(b) * (bounds.size.width/CGFloat(numberOfColumns))
            let y = CGFloat(a) * (bounds.size.height/CGFloat(numberOfRows))
            let width = bounds.size.width/CGFloat(numberOfColumns)/2
            let height = bounds.size.height/CGFloat(numberOfRows)/8
            let rect = CGRect(x: x + width/2, y: y/3 + height*3, width: width, height: height)
            //print(rect)
            let frame = UIView(frame: rect)
            blocks.append(frame)
            frame.backgroundColor = UIColor.blue()
            addSubview(frame)
            let item: UIDynamicItem = frame
            //gravity.addItem(item)
            collider.addItem(item)

            blockBehaviour.addItem(item)
            animator.addBehavior(blockBehaviour)





        }
    }
    let rectangle = CGRect(x: 0, y: 0, width: bounds.size.width, height: 3/2 * bounds.size.height)
    //print(rectangle)
    let boundary = UIBezierPath(rect: rectangle)
    collider.addBoundary(withIdentifier: "screen", for: boundary)

}

感谢您的帮助

最佳答案

你的 lazy var 不会工作。对象 (self) 不会神奇地举起手说“看着我,我是碰撞代表!”这不是委派的运作方式。

UICollisionBehavior 对象(实际上我在您的代码中没有看到它 - 它在哪里?)有一个 collisionDelegate 属性,this 必须是设置为 self

关于swift - CollisionBehavior 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925899/

相关文章:

arrays - 如何为泛型类型扩展数组?

ios - 快速解码嵌套的 jsons

iphone-sdk-3.0 - iPhone 上 UILabel 的字体大小可以通过流畅的动画来改变吗?

ios - 在 swift(iOS) 中的文本字段内添加前缀

Swift Xcode 组成员之间发生冲突,但组之间没有冲突

swift - 如何从 iOS 11 和 Swift 4 中的相机捕获深度数据?

ios - 如何在带有图像的 SwiftUI 中使用 Bundle

ios - SwiftUI - 表单中的 NavigationLink 单元格在详细信息弹出后保持突出显示

ios - UICollisionBehavior - UIView 碰撞的自定义形状