Я пытался запустить эту функцию CollisionBehavior.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. Шарик и блоки - это все динамические объекты, и у меня есть границы экрана, являющиеся границей. Поэтому каждый раз, когда блок или «мяч» попадает в другой блок, весло, мяч или конец экрана, он должен печатать «встречный», но ничего не печатается на терминале. Ниже приведен код блока, шара, весла и границ.
Paddle:
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)
}
Блоки и End Screen (граница):
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)
}
Спасибо за любую помощь
Вот рабочий, загружаемый пример из моей книги, показывающий, как настроить поведение столкновения: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch04p175dynamics/Dynamics/ViewController.swift – matt
Большое спасибо. Удивительный код тоже! –