【问题标题】:Detecting touch point in a line [closed]检测一行中的触摸点[关闭]
【发布时间】:2013-08-07 04:47:51
【问题描述】:

我无法在使用 UIBezierpath 绘制的线条中找到接触点。 CGPathContainPoint 不适用于线路。请帮帮我

【问题讨论】:

标签: ios uibezierpath cashapelayer


【解决方案1】:

您可以创建另一个路径对象来表示路径在屏幕上的实际外观,然后检查触摸点是否在该路径内。在CGPath reference 中,您会找到方便的构造函数CGPathCreateCopyByStrokingPath。你会像这样使用它:

CGPathRef originalPath = myBezierPath.CGPath;  //The single-line path

//Use the values you use to draw the path onscreen, 
//or use a width representing how far the user can touch
//for it to be recognized by the path.
//For example, for an error tolerance of 4px, use a width of 8px.
CGPathRef strokedPath = CGPathCreateCopyByStrokingPath(originalPath, NULL, lineWidth, lineCap, lineJoin, miterLimit);
BOOL pathContainsPoint = CGPathContainsPoint(strokedPath, NULL, touchPoint, NO);

如上所示,这让您可以指定一个区域让用户触摸而不是一条线。

希望这会有所帮助!

【讨论】:

  • 我想从一组形状中选择一条特定的线
  • 有什么问题?此代码仅确定一个点是否与一条路径相交。要从一组路径中选择一行,只需遍历路径并为每个路径运行此代码。
  • @architectpianist,如果我能给这 10 个赞,我会的。我一直在寻找一种方法来检测落在一条线上的点,这个解决方案非常有效。谢谢
猜你喜欢
  • 2013-03-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多