UIGraphicsGetCurrentContext()
vous donne un contexte dans lequel vous pouvez dessiner plusieurs choses. L'appeler line
n'est pas la bonne idée, car elle peut contenir plusieurs lignes, ou cercles, ou toutes sortes d'autres choses. Pensez au contexte en tant qu'artiste assis devant une toile vierge. Vous donnez des instructions à l'artiste, comme "dessinez une ligne rouge, puis tracez un cercle bleu". L'artiste suit les instructions et après, vous regardez la toile.
Voici comment dessiner une ligne, puis un cercle.
let context = UIGraphicsGetCurrentContext()
// Tell the context what stroked paths should look like
CGContextSetLineWidth(context, 3.0)
CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
// Draw a single line
CGContextMoveToPoint(context, 0, self.bounds.midY)
CGContextAddLineToPoint(context, self.bounds.width, self.bounds.midY)
CGContextStrokePath(context)
// Now draw a circle by filling a path.
// First, set the fill color:
CGContextSetFillColorWithColor(context, UIColor.blueColor().CGColor)
// Specify how big the circle is, and where its center is:
let circleRadius = CGFloat(5.0)
let circleCenter = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
// Then add a circle to the context, by specifying the rectangle that surrounds it:
let circleRect = CGRect(x: circleCenter.x - circleRadius,
y: circleCenter.y - circleRadius,
width: circleRadius * 2,
height: circleRadius * 2)
CGContextAddEllipseInRect(context, circleRect)
// And fill that circle:
CGContextFillPath(context)
Si vous voulez dessiner d'autres cercles, mais dans des endroits différents, il suffit d'appeler CGContextAddEllipseInRect
et CGContextFillPath
à nouveau, mais avec des valeurs différentes pour circleRect
. Selon ce que vous voulez, une boucle for pourrait être appropriée. C'est entièrement à vous.
Si vous ne voulez pas l'écrire vous-même, il existe de nombreuses bibliothèques de cartes tierces disponibles, il suffit de faire une recherche. Apple ne fournit pas un "officiel".