Changes

Jump to: navigation, search
Drawing a Line
<google>ADSDAQBOX_FLOW</google>
[[Image:ios_7_core_graphics_line.png|A line drawn with Core Graphics on iOS 7]]
CGContextStrokePath(context)
}
 
== Drawing Paths ==
 
As you may have noticed, in the above example we draw a single line by essentially defining the path between two points. Defining a path that comprises multiple points allows us to draw using a sequence of straight lines all connected to each other using repeated calls to the CGContextAddLineToPoint() function. Non-straight lines may also be added to a shape using calls to, for example, the CGContextAddArc() function.
 
The following code, for example, draws a diamond shape:
 
<pre>
override func drawRect(rect: CGRect)
{
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 2.0)
CGContextSetStrokeColorWithColor(context,
UIColor.blueColor().CGColor)
CGContextMoveToPoint(context, 100, 100)
CGContextAddLineToPoint(context, 150, 150)
CGContextAddLineToPoint(context, 100, 200)
CGContextAddLineToPoint(context, 50, 150)
CGContextAddLineToPoint(context, 100, 100)
CGContextStrokePath(context)
}
</pre>
 
When executed, the above code should produce output that appears as shown in Figure 59-3:
 
[[Image:ios_7_draw2d_path.png|An iOS Core Graphics Path drawing]]
 
Figure 59-3
== Drawing Paths ==

Navigation menu