Why sometimes the View pass through the boundary?

105 views Asked by At

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

0

There are 0 answers