簡體   English   中英

未調用UICollisionBehaviorDelegate方法

[英]UICollisionBehaviorDelegate methods not called

我使用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)

謝謝您的幫助!

這不是它的工作原理(可悲的是)。你的碰撞行為中只有一個項目你要求它檢測碰撞與什么??? 如果您添加了第二個項目,您將看到委托被調用..

真正的問題是為什么? 好吧,碰撞檢測一個項目的框架是否與另一個框架的框架相交。如果它做了你要求的( rectphysicsView一個孩子),那么它總是會碰撞(這不是你想要的)。

由於你的physicsView不是碰撞行為的一部分,你什么也得不到。你告訴它限制項目在其范圍內( setTranslatesReferenceBoundsIntoBoundary對添加到行為本身的項目起作用),但不檢測與邊界的碰撞。 例如,如果項目在physicsView之外,它將無法進入。如果它在里面,它就無法離開。 但它不會被視為每次碰撞(否則它總是碰撞)。

為了檢測rectphysicsView之間的沖突,你需要給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")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM