为什么有时视图会通过边界?

时间:2018-04-08 12:19:47

标签: swift uikit-dynamics

我有一个方形视图,当我放下它时有时会穿过一条线的边界,如果该线是水平的则没有问题,但是它的某些位置会导致问题。
这是我的代码:

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)


线形:

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描述概率:**

enter image description here

 **另一个Gif描述了边界问题:


enter image description here

0 个答案:

没有答案