开发者

How to import x,y coordinate data from xml file and use it for drawing a path ?

开发者 https://www.devze.com 2023-03-25 18:37 出处:网络
my question is.... I can read data (x,y开发者_JS百科 coordinate) from xml file, but, i don\'t understand how i can put them in a drawrect method for drawing paths. In the specific, i have build, by i

my question is....

I can read data (x,y开发者_JS百科 coordinate) from xml file, but, i don't understand how i can put them in a drawrect method for drawing paths. In the specific, i have build, by interface builder, a label, a view and a button. My goal is: Click on the button and paths are drawn.

I would like to know how to link xml data and drawrect method .

Thx for helping me, and sorry for my easy english (i'm italian)


First you need to keep the coordinates you read from the XML around in some data structure, maybe an NSArray. Since NSArray only wraps Objective-C objects, you can't simply add an NSPoint to it. You need to wrap the point inside the NSValue object using [NSValue valueFromPoint: …].

If you have your array of points, in the drawRect: of your view you could write:

NSBezierPath * path = [NSBezierPath path];
BOOL first = YES;
for (NSValue * v in points) {
    if (first)
        [path moveToPoint:[v pointValue]];
    else
        [path lineToPoint:[v pointValue]];
    first = NO;
}
[path closePath];
[path stroke];

This will use the points stored in the NSArray called points to fill an NSBezierPath with information. NSBezierPath is the class used for describing geometric figures and drawing them.

If you don't want the first and the last point to be connected by a line, remove the [path closePath] instruction. If you'd rather have your path filled with the current color, change [path stroke] to [path fill].

If you want to change the color of your path, put a [[NSColor blueColor] set] before the stroke/fill command, substituting your preferred color.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号