【问题标题】:NSBezierPath containsPoint: is too sensitive when path not closedNSBezierPath containsPoint: 路径未关闭时过于敏感
【发布时间】:2013-12-20 02:31:38
【问题描述】:

我对 containsPoint 方法有疑问。我画了一些盒子和盒子之间的连接器。连接器基本上是一条曲线,基于单个 curveToPoint:controlPoint1:controlPoint2 调用。当我现在尝试用鼠标选择这条曲线/路径时,这很难做到。 containsPoint: 方法似乎非常敏感。我试图把线画得更大(setLineWidth:),但这似乎没有帮助。

有什么想法我需要做不同的事情吗?

【问题讨论】:

  • 敏感如检测到多次触摸或很难点击?
  • 标签iOSNSBezierPath 是矛盾的。是 iOS 和 UIBezierPath 还是 NSBezierPath 和 OS X?
  • 不确定 iOS 是如何出现在这里的。一定是忽略了。
  • 敏感,很难点击。

标签: objective-c nsbezierpath


【解决方案1】:

对于 CGPath,您始终可以使用以下方法创建一个闭合路径,它是描边路径的轮廓:

CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(
    path,             // your original CGPathRef
    NULL,             // don't transform
    10.0,             // lineWidth
    kCGLineCapButt,   // lineCap    (default value)
    kCGLineJoinMiter, // lineJoin   (default value)
    0.0               // miterLimit
);

您可以阅读更多关于路径命中测试here (by Ole Begemann)here (by Rob Napier) 的信息。

【讨论】:

  • 这正是我所需要的。谢谢你。最终解决方案作为不同的答案提供,但只有在大卫的提示下才有可能。
【解决方案2】:

感谢大卫的回答,我现在可以提供完整的答案。 我需要的是三个部分。

  1. 将 NSBezierPath 转换为 CGPath。这可以按照 Apple 文档中的规定进行。或者您可以使用https://github.com/iccir/XUIKit‎库,它将 iPhone 框架功能添加到 MacOS 框架中。
  2. 按照 David 的建议使用 CGPathCreateCopyByStrokingPath 函数。
  3. 将新的 CGPath 转换为 NSBezierPath。 David 与 Ole Begemann 的块的链接非常有助于展示如何做到这一点。不过,XUIKit 再次领先一步,提供了+(NSBezierPath) bezierPathWithCGPath: 函数

结果如下所示。

//con as Connector was the starting point
CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(con.CGPath, NULL, 4, kCGLineCapButt, kCGLineJoinBevel, kCGLineJoinMiter );
NSBezierPath * hitPath = [NSBezierPath bezierPathWithCGPath:tapTargetPath];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    相关资源
    最近更新 更多