Question

I have this code that draws a simple zigzag line. The initial point onX axis is 0. The problem is that I am trying to offset the initial point by 20pts, but I just can't get it right.

I've tried both CGRectInset and CGRectOffSet, but didn't work..

Here's my code:

- (UIBezierPath *)pathFromDataInRect:(CGRect)rect {

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, CGRectGetMidY(rect))];
[path addLineToPoint:CGPointMake((CGRectGetWidth(rect)/3), CGRectGetMaxY(rect))];
[path addLineToPoint:CGPointMake((CGRectGetWidth(rect)/3)*2, CGRectGetMinY(rect))];
[path addLineToPoint:CGPointMake((CGRectGetWidth(rect)/3)*3, CGRectGetMidY(rect))];
UIColor *startColor = [UIColor redColor];
[startColor setStroke];
[path stroke];
return path;
}


- (CGRect)closingDataRect {

return CGRectMake(0, 150, 200, 100);
}


- (void)drawRect:(CGRect)rect
{

CGRect dataRect = [self closingDataRect];

CGRect dataRectOffset = CGRectOffset(dataRect, 20, 0);

[self pathFromDataInRect:dataRectOffset];


}

The first pic is what I have when I run the code and the second pic is what I want.

Pic of what I have

Pic of what I want

Was it helpful?

Solution

Your first moveToPoint call hard-codes the x position to 0. If you want to start at the right edge of your rectangle, shouldn't you change that first moveToPoint line to pass in your rect's origin.x as the starting point?

Then you also need to change any code that calculates points in your path purely from the width/height use width+origin.x or height+origin.y, so when you shift the origin of your rectangle, all the points in the rectangle shift as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top