2

I have a square view , when I drop it sometimes pass through a boundary of a line , if the line is horizontal there is no problem , but some position of it cause the problem.
Here is my codes:

square = UIView(frame: CGRect(x: 100, y: 30, width: 100, height: 100))
    square.backgroundColor = UIColor.gray
    view.addSubview(square)

    animator = UIDynamicAnimator(referenceView: view)
    gravity = UIGravityBehavior(items: [square])
    gravity.gravityDirection = CGVector(dx: 0, dy: 0.1)
    animator.addBehavior(gravity)

    collision = UICollisionBehavior(items: [square])
    collision.collisionDelegate = self

    collision.addBoundary(withIdentifier: "line" as NSCopying, for: line.collisionBoundingPath)
    collision.translatesReferenceBoundsIntoBoundary = true
    animator.addBehavior(collision)

    let itemBehaviour = UIDynamicItemBehavior(items: [square])
    itemBehaviour.elasticity = 0.6
    animator.addBehavior(itemBehaviour)


Line Shape:

let startingPoint = CGPoint(x: 0, y: 150)
    path.move(to: startingPoint)
    path.addLine(to: CGPoint(x: 250, y: 250))
    UserPath = path

    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = UIColor.blue.cgColor
    shapeLayer.lineWidth = 3.0

    layer.addSublayer(shapeLayer)

enter image description here


Gif describe the prob: **

enter image description here

**Another Gif describe a problem with the boundary:


enter image description here

Hassan Taleb
  • 2,350
  • 2
  • 19
  • 24
  • What happens when you get rid of the line, remove `collision.translatesReferenceBoundsIntoBoundary = true` and use this as your collision boundary? `collision.addBoundary(withIdentifier: "boundary" as NSCopying, from: CGPoint(x: 0, y: 100), to: CGPoint(x: 200, y: 300))` – trndjc Apr 08 '18 at 16:15
  • @slickdaddy same results – Hassan Taleb Apr 08 '18 at 16:18

0 Answers0