swift UIBezierPath 碰撞边界问题(UIKit 动力学)
swift UIBezierPath collision boundary issues ( UIKit Dynamics )
您好,我有一个应用程序,用户可以在其中用手指绘制一条贝塞尔曲线路径,并将其指定为坠落物体的碰撞边界。它有效!...但是!贝塞尔曲线似乎正在关闭路径(隐藏),这也成为边界。
所以,如果我画一条凸曲线,它就完美了。但是如果我画一条凹曲线,物体就不会进入曲线。任何帮助表示赞赏。我需要以某种方式打开曲线!下面的代码
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
animator = UIDynamicAnimator(referenceView: self)
gravity = UIGravityBehavior()
animator.addBehavior(gravity)
gravity.gravityDirection = CGVector(dx: 0, dy: 0.1)
collision = UICollisionBehavior(items: [hamburger])
collision.collisionDelegate = self
collision.addBoundary(withIdentifier: "belt" as NSCopying, for: UIBezierPath(rect: belt.frame))
animator.addBehavior(collision)
collision.addItem(hamburger)
gravity.addItem(hamburger)
collision.translatesReferenceBoundsIntoBoundary = true
let itemBehaviour = UIDynamicItemBehavior(items: [hamburger])
itemBehaviour.elasticity = 0.1
animator.addBehavior(itemBehaviour)
lines.forEach { (line) in
for (i, p) in (line.points?.enumerated())! {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
context.setStrokeColor(line.color?.withAlphaComponent(line.opacity ?? 1.0).cgColor ?? UIColor.systemBlue.cgColor)
context.setLineWidth(line.width ?? 10.0)
lineboundary = UIBezierPath(cgPath: context.path!)
collision.addBoundary(withIdentifier: "linedrawn" as NSCopying, for: lineboundary)
}
context.setLineCap(.round)
context.strokePath()
}
}
UIKit Dynamics 不允许凹路径。但您可以将它们添加为单独的 UICollisionBehavior
个实例:
for index in points.indices.dropFirst() {
let path = UIBezierPath()
path.move(to: points[index-1])
path.addLine(to: points[index])
let collision = UICollisionBehavior(items: [droppedView])
collision.addBoundary(withIdentifier: "\(index)" as NSString, for: path)
animator.addBehavior(collision)
}
这导致:
您好,我有一个应用程序,用户可以在其中用手指绘制一条贝塞尔曲线路径,并将其指定为坠落物体的碰撞边界。它有效!...但是!贝塞尔曲线似乎正在关闭路径(隐藏),这也成为边界。
所以,如果我画一条凸曲线,它就完美了。但是如果我画一条凹曲线,物体就不会进入曲线。任何帮助表示赞赏。我需要以某种方式打开曲线!下面的代码
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
animator = UIDynamicAnimator(referenceView: self)
gravity = UIGravityBehavior()
animator.addBehavior(gravity)
gravity.gravityDirection = CGVector(dx: 0, dy: 0.1)
collision = UICollisionBehavior(items: [hamburger])
collision.collisionDelegate = self
collision.addBoundary(withIdentifier: "belt" as NSCopying, for: UIBezierPath(rect: belt.frame))
animator.addBehavior(collision)
collision.addItem(hamburger)
gravity.addItem(hamburger)
collision.translatesReferenceBoundsIntoBoundary = true
let itemBehaviour = UIDynamicItemBehavior(items: [hamburger])
itemBehaviour.elasticity = 0.1
animator.addBehavior(itemBehaviour)
lines.forEach { (line) in
for (i, p) in (line.points?.enumerated())! {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
context.setStrokeColor(line.color?.withAlphaComponent(line.opacity ?? 1.0).cgColor ?? UIColor.systemBlue.cgColor)
context.setLineWidth(line.width ?? 10.0)
lineboundary = UIBezierPath(cgPath: context.path!)
collision.addBoundary(withIdentifier: "linedrawn" as NSCopying, for: lineboundary)
}
context.setLineCap(.round)
context.strokePath()
}
}
UIKit Dynamics 不允许凹路径。但您可以将它们添加为单独的 UICollisionBehavior
个实例:
for index in points.indices.dropFirst() {
let path = UIBezierPath()
path.move(to: points[index-1])
path.addLine(to: points[index])
let collision = UICollisionBehavior(items: [droppedView])
collision.addBoundary(withIdentifier: "\(index)" as NSString, for: path)
animator.addBehavior(collision)
}
这导致: