1

Possible Duplicate:
iOS Draw Rectagle with curved ends

In Objective-C (an iPhone app), how would I write a function which takes in a CGRect and draws a one-sided rounded rectangle. This is what a one sided rounded rectangle looks like:

ooooooooooooooo
o               o
o                 o
o                o
o               o
ooooooooooooooo

That drawing is hard to see, but basically, it's like a pencil, just not as pointy at the round side. I would like to use a combination of CGContext methods to do this. Can someone provide this code?

Community
  • 1
  • 1
StanLe
  • 5,037
  • 9
  • 38
  • 41
  • [This one](http://stackoverflow.com/questions/1031930/how-is-a-rounded-rect-view-with-transparency-done-on-iphone/1031936#1031936) tells you how to draw a rounded rect with all 4 corners rounded -- it shouldn't be hard to adapt that so 2 of the corners are sharp. – Daniel Dickison Jul 01 '11 at 23:39
  • Thank you Daniel. I tried to use that code but couldn't figure it out. I just want one rounded side on the rectangle (the right side). I just can't figure out how to use the CGContextAddArcToPoint method. – StanLe Jul 01 '11 at 23:44
  • Glad to help. Don't be afraid to just try stuff, though. Particularly when it comes to drawing, the worst that will happen is that you draw something that's not quite what you want, and whatever does get drawn will teach you something about how the function in question works. – Caleb Jul 02 '11 at 00:47

1 Answers1

1

UIBezierPath provides a number of methods for constructing all sorts of paths. If that's not enough, CoreGraphics (aka Quartz) provides a complete set of path operations. Either of these solutions will be more than sufficient for drawing the path you've described.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I have seen these methods but still can't figure it out. Can you explain the CGContextAddArcToPath method? I just can't seem to figure out how to use that one. What do the parameters mean? – StanLe Jul 01 '11 at 23:45
  • Guessing you mean [CGContextAddArcToPoint](http://tinyurl.com/3cj23h2), right? The parameters are explained right there. You supply two lines that form an angle, so you need three points: the start point, the vertex, and the end point. The start point is the current point in the path, so you only need to specify the end point of the first line (the vertex) and the end point of the second line. You also supply the radius. Try it out with some different values and you'll quickly see how it works. – Caleb Jul 02 '11 at 00:08