I have an custom UIView
. I keep on the lines and points when the finger touches moves. Then I make points to CGPath
with this method:
- (void)makeCGPath
{
CGMutablePathRef path = CGPathCreateMutable();
if (lines&& lines.count>0)
{
for (int i = 0; i < lines.count; i++)
{
CGMutablePathRef linePath = CGPathCreateMutable();
NSArray *tempArray = [lines objectAtIndex:i];
CGPoint p = [[tempArray objectAtIndex:0]CGPointValue];
CGPathMoveToPoint(linePath, NULL, p.x, p.y);
for (int j = 1; j < tempArray.count; j++)
{
p = [[tempArray objectAtIndex:j]CGPointValue];
CGPathAddLineToPoint(linePath, NULL, p.x, p.y);
}
CGPathAddPath(path, NULL, linePath);
CGPathRelease(linePath);
}
}
if (points&& points.count > 0)
{
CGMutablePathRef pointPath = CGPathCreateMutable();
CGPoint p = [[points objectAtIndex:0]CGPointValue];
CGPathMoveToPoint(pointPath, NULL, p.x, p.y);
for (int i = 1; i < points.count;i++ )
{
p = [[points objectAtIndex:i]CGPointValue];
CGPathAddLineToPoint(pointPath, NULL, p.x, p.y);
}
CGPathAddPath(path, NULL, pointPath);
CGPathRelease(pointPath);
}
drawPath = CGPathCreateCopy(path);
[self setNeedsDisplay];
[lines removeAllObjects];
[points removeAllObjects];
}
And my drawRect is look like this follow.
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 3);
[[UIColor blackColor]set];
CGContextAddPath(ctx, drawPath);
CGContextStrokePath(ctx);
}
It seems so strange that it displays only the last line on the screen. What's wrong with it?
How can I keep all of these paths on screen?
I have the exact same problem. It's not working on iOS 5.0 but it does work on iOS 5.1 and more. I'm still looking for the fix.
UPADTE: I fixed the issue using UIBezierPath. You can draw all your paths using Besier and then convert it to CGPath this way:
UIBezierPath *path = [[UIBezierPath alloc] init];
// Add all the lines you need
[path addLineToPoint:p];
// Or points
[path moveToPoint:p];
// Append paths
[path appendPath:linePath];
// Make a CGPath from UIBezierPath
CGPath myCGPath = path.CGPath;
Try this code, it should work but I haven't tested it:
- (void)makeCGPath
{
UIBezierPath *path = [[UIBezierPath alloc] init];
if (lines && lines.count>0)
{
for (int i = 0; i < lines.count; i++)
{
UIBezierPath *linePath = [[UIBezierPath alloc] init];
NSArray *tempArray = [lines objectAtIndex:i];
CGPoint p = [[tempArray objectAtIndex:0]CGPointValue];
[linePath addLineToPoint:p];
for (int j = 1; j < tempArray.count; j++)
{
p = [[tempArray objectAtIndex:j]CGPointValue];
[linePath addLineToPoint:p];
}
[path appendPath:linePath];
}
}
if (points && points.count > 0)
{
UIBezierPath *pointPath = [[UIBezierPath alloc] init];
CGPoint p = [[points objectAtIndex:0]CGPointValue];
[pointPath moveToPoint:p];
for (int i = 1; i < points.count;i++ )
{
p = [[points objectAtIndex:i]CGPointValue];
[pointPath moveToPoint:p];
}
[path appendPath:pointPath];
}
drawPath = path.CGPath;
[self setNeedsDisplay];
[lines removeAllObjects];
[points removeAllObjects];
}