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