Hi,
I am trying to made a flow chart application and need to draw lines and curves.
I can manage drawing straight lines with really thin rectangles/quads, but i dont see a way of drawing curves/béziers.
Does anyone have any suggestions?
Hi,
I am trying to made a flow chart application and need to draw lines and curves.
I can manage drawing straight lines with really thin rectangles/quads, but i dont see a way of drawing curves/béziers.
Does anyone have any suggestions?
You can draw Core Graphics into a SPTexture to achieve anti-aliasing lines, arcs, and so forth. When you say "curves", do you mean rounded ends on the lines or a curve between points?
Hi shilo, i meant curves between 2 points. specifically something like bezier curves.
http://en.wikipedia.org/wiki/B%C3%A9zier_curve
So if i understand your suggestion correctly, i should be able to draw lines using native cocoa methods and then create a SPTexture from than?
Thanks!
You can draw Core Graphics into an SPTexture with the draw block inside the initialization.
Here is an example: (curtesy of Daniel)
SPTexture *cgTexture = [[SPTexture alloc] initWithWidth:256 height:128 draw:^(CGContextRef context) { // draw a string CGContextSetGrayFillColor(context, 1.0f, 1.0f); NSString *string = @"Hello Core Graphics"; [string drawAtPoint:CGPointMake(20.0f, 20.0f) withFont:[UIFont fontWithName:@"Arial" size:25]]; }];
There are a few ways to create lines and curves in Core Graphics. This should get you started: http://developer.apple.com/library/ios/.../BezierPaths.html. Feel free to post here if you need help. I haven't used arcs, but I have worked with Core Graphics.
Thanks very much Shilo.. ill try this out!
On a side note, I would warmly welcome a class in the next sparrow release such as SPLine, which basically draws a line with a specific thickness between specified SPPoints.
I believe Shilo already made that class.
Wow, that's exactly what I wanted. Amazing stuff Shilo
Should be a part of the default Sparrow package.
I'll consider it, thanks =)
Hi, this might not be directly related to the sparrow lib, but i am trying to set the stroke color of a UIBezierPath.
this is what i have so far:
SPTexture *cgTexture = [[SPTexture alloc] initWithWidth:472 height:266 draw:^(CGContextRef context) { UIBezierPath* aPath = [UIBezierPath bezierPath]; [aPath moveToPoint:CGPointMake(160, 133)]; // Draw the line [aPath addCurveToPoint:CGPointMake(10, 40 ) controlPoint1:CGPointMake( 160,100) controlPoint2:CGPointMake(160 ,40)]; [aPath stroke]; }];
This draws a black stroke, how do i set a new color? I cant seem to find a way of doing that.
I believe you are looking for this.
Change redColor and 0xFF0000 to whatever you like.
CGColorRef redColor = [UIColor colorWithRed:SP_COLOR_PART_RED(0xFF0000)/255.0 green:SP_COLOR_PART_GREEN(0xFF0000)/255.0 blue:SP_COLOR_PART_BLUE(0xFF0000)/255.0 alpha:1].CGColor; // Not sure which of these to use CGContextSetFillColorWithColor(context, redColor); CGContextSetStrokeColorWithColor(context, redColor); // Draw stuff here
oops i posted that previous comment too soon... i figured out how to set stroke color.
Incase anyone else has this issue in the future, here is how its done:
SPTexture *cgTexture = [[SPTexture alloc] initWithWidth:472 height:266 draw:^(CGContextRef context) { //white color with alpha 1 CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); UIBezierPath* aPath = [UIBezierPath bezierPath]; [aPath moveToPoint:CGPointMake(160, 133)]; // Draw the line [aPath addCurveToPoint:CGPointMake(10, 40 ) controlPoint1:CGPointMake( 160,100) controlPoint2:CGPointMake(160 ,40)]; [aPath stroke]; }];
thanks enbr, yes that works as well..
You must log in to post.