Swift UibezierPath碰撞边界问题(UIKIT动力学)

发布于 2025-01-20 18:03:23 字数 1974 浏览 2 评论 0原文

嗨,我有一个应用程序,用户可以用手指绘制一个更偏见的路径,并将其分配为落下对象的碰撞边界。它有效!...但是!似乎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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2025-01-27 18:03:23

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)
}

结果:

UIKit Dynamics does not permit concave paths. But you can add them as separate UICollisionBehavior instances:

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)
}

That results in:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文