Question

I have an array of coordinates, x and y like

NSArray *objectCoords = [NSArray arrayWithObjects: @"{{116,371},{85,42}}", @"{{173,43},{85,42}}", @"{{145,200},{85,42}}",

I am trying to draw a circle using drawRect.like:

-(void)drawRect:(CGRect)rect
{

   // [super drawRect:rect];  144,179,130
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 10.0);
    CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);



    int x=10;
    int y=10;
    CGRect rectangle0;
    for (int i=0; i<=10; i++) {
     rectangle0 = CGRectMake(x,y,10,10);//just to draw
        x=x+20;
        y=y+23;
        NSLog(@"x: %d  and y: %d", x,y);
         CGContextAddEllipseInRect(context, rectangle0);
    }

}

I am not sure, how to put these coordinates of array in the CGRect to draw circle at those particular coordinates.

Please guide.

Thanks

Was it helpful?

Solution

You can create a CGRect object from a string representation using CGRectFromString(string). This takes strings in the format {{x,y},{w, h}} (which thankfully your strings are!)

For example:

NSArray *objectCoords = [NSArray arrayWithObjects: @"{{116,371},{85,42}}", @"{{173,43},{85,42}}", @"{{145,200},{85,42}}",

for (NSString* objectCoord in objectCoords) {
    CGRect coord = CGRectFromString(objectCoord);
    // Draw using your coord
}

Conversely, if you have a CGRect, you can get a string representation via NSStringFromCGRect(rect)

OTHER TIPS

Use CGRectFromString to convert such NSStrings to CGRects

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