Swift UibezierPath碰撞边界问题(UIKIT动力学)
嗨,我有一个应用程序,用户可以用手指绘制一个更偏见的路径,并将其分配为落下对象的碰撞边界。它有效!...但是!似乎Bézier曲线正在关闭路径(隐藏),这也成为边界。
因此,如果我绘制凸曲线,则可以完美。但是,如果我绘制凹面曲线,则对象将不会进入曲线。任何帮助都赞赏。我需要以某种方式打开曲线!下面的代码
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()
}
}
Hi I have an app where the user can draw a bezier path with finger and it assigns this as a collision boundary for falling objects. It works!... But! It appears the Bézier curves are closing the path (hidden) and this also becomes a boundary.
So if I draw a convex curve it works perfect. But if I draw a concave curve the object will not enter the curve. Any help appreciated. I need to open the curves somehow! code below
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()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Uikit Dynamics不允许凹入路径。但是您可以将它们添加为单独的
uicollisionbehavior
实例:结果:
UIKit Dynamics does not permit concave paths. But you can add them as separate
UICollisionBehavior
instances:That results in: