检测线中的触摸点

时间:2013-08-07 04:47:52

标签: ios uibezierpath cashapelayer

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

1 个答案:

答案 0 :(得分:7)

您可以创建另一个路径对象来表示路径在屏幕上的实际外观,然后检查触摸点是否在该路径内。在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);

如上所示,这为您提供了指定用户触摸而不是线条的区域的好处。

希望这有帮助!