2016-03-26 14 views
0

私は画面上に水平な中心(実線を表す)を持つカスタムビューを管理するカスタムクラスを持っています。そして、私はこのようなラインを描いた:1行に複数の円を描くCGGraphicsContext

override func drawRect(rect: CGRect) 
    { 
    let line = UIGraphicsGetCurrentContext() 
      CGContextSetLineWidth(line, 3.0) 
      CGContextSetStrokeColorWithColor(line, UIColor.redColor().CGColor) 

      CGContextMoveToPoint(line, 0, self.bounds.midY) 
      CGContextAddLineToPoint(line, self.bounds.width, self.bounds.midY) 

      CGContextStrokePath(line) 
    }  

は、しかし、私はこの行に複数の固体円を描くと、チャート上の点のようにそれを見てみてくださいする必要があります。私は実際にミニチャート表現をしようとしています。どのようにしてサークルを描くことができますか?ネストされた 'for'ループまたは? Appleの公式チャートAPIはありますか?

答えて

1

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のために再度CGContextAddEllipseInRectCGContextFillPathが、異なる値で呼び出したい場合

。あなたが望むものに応じて、forループが適切かもしれません。それはあなた次第です。

自分で書きたくない場合は、多数のサードパーティのチャートライブラリが利用できます。検索するだけです。 Appleは「公式」ものを提供していません。