UIGraphicsGetCurrentContext()
は、複数のものを描画できるコンテキストを提供します。 line
と呼ぶのは、複数の線や円、その他あらゆる種類のものが含まれている可能性があるため、正しい考えではありません。
空白のキャンバスの前に座っているアーティストとしてのコンテキストを考えてみましょう。あなたは、「赤い線を引いてから青い円を描く」ような指示をアーティストに与えます。アーティストは指示に従い、その後キャンバスを見ます。
ここでは、線を描いてから円を描く方法を説明します。
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)
あなたはより多くの円を描くが、別の場所で、ちょうどcircleRect
のために再度CGContextAddEllipseInRect
とCGContextFillPath
が、異なる値で呼び出したい場合
。あなたが望むものに応じて、forループが適切かもしれません。それはあなた次第です。
自分で書きたくない場合は、多数のサードパーティのチャートライブラリが利用できます。検索するだけです。 Appleは「公式」ものを提供していません。