2

I am not able to find the touch point in the line drawn using UIBezierpath. CGPathContainPoint is not working for line. Please help me out

Shruthi
  • 45
  • 3
  • http://stackoverflow.com/questions/10831370/how-to-check-if-touch-point-is-on-uibezierpath-ios can you check this link – Jitendra Aug 07 '13 at 04:59

1 Answers1

7

You can create another path object that represents how the path actually looks on screen, then check if the touch point is inside that path. In the CGPath reference, you'll find the handy constructor function CGPathCreateCopyByStrokingPath. You would use it somewhat like this:

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

As shown above, this gives you the benefit of specifying a region for the user to touch instead of a line.

Hope this helps!

Community
  • 1
  • 1
architectpianist
  • 2,562
  • 1
  • 19
  • 27
  • 1
    I want to select a particular line from a set of shapes – Shruthi Aug 07 '13 at 05:40
  • What's the problem? This code just determines if one point intersects one path. To select a line from a set of paths, just loop through the paths and run this code for each of them. – architectpianist Aug 08 '13 at 10:34
  • @architectpianist, If I could give this 10 upvotes I would. I've been looking all over for a way to detect a point falling on a single line and this solution works perfectly. Thanks – sbru Sep 16 '15 at 21:14