我在应用程序上工作并使用UIKitDynamics实现了碰撞检测。
碰撞检测正在进行中。但不知何故,没有调用UICollisionBehaviorDelegate方法。 没有显示警告或错误。
我创建了一个示例项目来演示这个问题:
import UIKit
class ViewController: UIViewController {
private var dynamicAnimator: UIDynamicAnimator?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.initPhysics()
}
fileprivate var initalized = false
fileprivate func initPhysics() {
if initalized {
return
}
initalized = true
let physicsView = UIView(frame: view.bounds.insetBy(dx: 40, dy: 40))
physicsView.backgroundColor = UIColor.black
self.dynamicAnimator = UIDynamicAnimator(referenceView: physicsView)
physicsView.isUserInteractionEnabled = false
view.addSubview(physicsView)
//Create ball
let frame = CGRect(x: 0, y: 0, width: 40, height: 40)
let rect = UIView(frame: frame)
rect.backgroundColor = UIColor.red
physicsView.addSubview(rect)
let behavior = UIDynamicItemBehavior(items: [rect])
behavior.elasticity = 1.0
behavior.resistance = 0.0
behavior.friction = 0.0
behavior.allowsRotation = false
self.dynamicAnimator?.addBehavior(behavior)
//collision behavior setup
let collisionBehavior = UICollisionBehavior(items: [rect])
collisionBehavior.collisionDelegate = self
collisionBehavior.setTranslatesReferenceBoundsIntoBoundary(with: .zero)
collisionBehavior.collisionMode = .everything
self.dynamicAnimator?.addBehavior(collisionBehavior)
//Push ball
let pushBehavior = UIPushBehavior(items: [rect], mode: UIPushBehaviorMode.instantaneous)
pushBehavior.active = true
pushBehavior.pushDirection = CGVector(dx: 0.5, dy: 0.5)
self.dynamicAnimator?.addBehavior(pushBehavior)
}
}
extension ViewController: UICollisionBehaviorDelegate {
// DELEGATE METHODS NOT CALLED
func collisionBehavior(_ behavior: UICollisionBehavior,
beganContactFor item: UIDynamicItem,
withBoundaryIdentifier identifier: NSCopying?,
at p: CGPoint) {
print("began contact boundary")
}
func collisionBehavior(_ behavior: UICollisionBehavior,
endedContactFor item: UIDynamicItem,
withBoundaryIdentifier identifier: NSCopying?) {
print("contact boundary ended")
}
func collisionBehavior(_ behavior: UICollisionBehavior,
endedContactFor item1: UIDynamicItem,
with item2: UIDynamicItem) {
print("contact item ended")
}
func collisionBehavior(_ behavior: UICollisionBehavior,
beganContactFor item1: UIDynamicItem,
with item2: UIDynamicItem,
at p: CGPoint) {
print("began contact item")
}
}
基础SDK:iOS 11.2,Xcode版本9.2(9C40b)
感谢您的帮助!
答案 0 :(得分:3)
这不是它的工作方式(遗憾的是)..你的碰撞行为只有一个项目而你要求它检测碰撞与什么???如果你添加了第二个项目,你会看到委托被调用..
真正的问题是为什么?好吧,碰撞检测一个项目的框架是否与另一个项目的框架相交。如果它符合您的要求(rect
是physicsView
的孩子),那么它将始终发生碰撞(其中不是你想要的... ..
由于您的physicsView
不是碰撞行为的一部分,因此您什么也得不到。您告诉它将项目限制在其范围内(setTranslatesReferenceBoundsIntoBoundary
适用于添加到行为本身的项目) ,但不是检测与边界的碰撞。例如,如果项目在physicsView
之外,它将无法进入。如果它在里面,它就无法离开。但它不会被视为每次碰撞(否则它总是碰撞)。
为了检测rect
和physicsView
之间的冲突,你需要给physicsView
一些物理属性..现在它只是一个普通的UIView
(参考)视图)..
另一种必须为其提供物理属性的方法是为其添加边界:
collisionBehavior.addBoundary(withIdentifier: "PhysicsViewBoundary" as NSString, for: UIBezierPath(rect: physicsView.bounds))
现在它将调用:
func collisionBehavior(_ behavior: UICollisionBehavior,
beganContactFor item: UIDynamicItem,
withBoundaryIdentifier identifier: NSCopying?,
at p: CGPoint) {
print("began contact boundary")
}
func collisionBehavior(_ behavior: UICollisionBehavior,
endedContactFor item: UIDynamicItem,
withBoundaryIdentifier identifier: NSCopying?) {
print("contact boundary ended")
}