gpt4 book ai didi

swift - CollisionBehavior 未被调用

转载 作者:行者123 更新时间:2023-11-28 06:36:38 25 4
gpt4 key购买 nike

我一直在尝试让这个 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/

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