调试代码:如何给 CGContext 中的线条着色?

Debugging code:: How to colors lines in CGContext?

以下代码有问题,由于某些原因,应该在 UIColor.yellowColor()

中绘制的径向线被绘制成黑色
import UIKit
import QuartzCore


let π:CGFloat = CGFloat(M_PI)

class CircularPlotView:UIView {

  var lineWidth:CGFloat = 1.5 { didSet { setNeedsDisplay() } }
  var edgeColor:UIColor = UIColor.yellowColor() { didSet { setNeedsDisplay() } }
  var fillColor:UIColor = UIColor.orangeColor() { didSet { setNeedsDisplay() } }
  var fieldColor:UIColor = UIColor.yellowColor(){ didSet { setNeedsDisplay() } }
  var lineColor:UIColor = UIColor.blueColor(){ didSet { setNeedsDisplay() } }
  var scale: CGFloat = 0.90 { didSet { setNeedsDisplay() } }
  //var plotCenter:CGPoint { return convertPoint(center, fromView: superview) }
  var plotCenter:CGPoint {return CGPoint(x: 190,y: 316) }
  var plotRadius:CGFloat {return min(bounds.size.width, bounds.size.height) / 8 * scale }
  var diameterX:CGFloat = 100.0
  var radiusX:CGFloat {return diameterX * 0.5}
  var scaleFactorDB:CGFloat = 5.0
  var offsetDB:CGFloat = 0.0

  //data to plot
  var dataString:[String] = ["cat","dog","pizza"]
  var dataDB:[Float] = [11.0,9.0,3.5]

  var fontSize:CGFloat = 17.0

  override func drawRect(rect: CGRect) {

    let context = UIGraphicsGetCurrentContext()

    let fieldFont = UIFont.systemFontOfSize(fontSize)

    let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.alloc()
    paragraphStyle.lineHeightMultiple = 1
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping

    let attributes:Dictionary = [
      NSParagraphStyleAttributeName : paragraphStyle,
      NSForegroundColorAttributeName: fieldColor,
      NSFontAttributeName: fieldFont
    ]


    var theta:CGFloat = 1.5 * π //CGFloat(M_PI)
    var radius:CGFloat = 0.0
    //var s:NSString = "ERROR FIX DATABASE" //if shown error in DB

    let startTime = CFAbsoluteTimeGetCurrent()

    //MARK:== draw strings ==
    //with enough separation so they don't clobber each other
    var myAttributedString:NSMutableAttributedString!

    for (var i=0; i < dataDB.count; i++)
    {
      //s = NSString(string: dataString[i])
      myAttributedString = NSMutableAttributedString (string: dataString[i], attributes:attributes)

      theta =  1.97 * π * ( CGFloat(i) / (CGFloat(dataDB.count) ) )
      radius =  ( radiusX + 10.0 +  offsetDB + scaleFactorDB*CGFloat(dataDB.reduce(dataDB[0], { max([=10=], ) })) )

      CGContextSaveGState(context)
      CGContextTranslateCTM(context,plotCenter.x ,plotCenter.y )
      CGContextRotateCTM(context, theta)
     CGContextSetStrokeColorWithColor(context,UIColor.yellowColor().CGColor)
      CGContextMoveToPoint(context,0.0,0.0)
      CGContextAddLineToPoint(context, radiusX + scaleFactorDB*CGFloat(dataDB[i]) + offsetDB, 0.0 )
      myAttributedString.drawAtPoint(CGPoint(x: radius , y: 0 ))
      CGContextRestoreGState(context)
    }

    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
    println("Time elapsed loop through lines and strings: \(timeElapsed) s")

    CGContextStrokePath(context)

    //MARK:== draw oval, fill, edge ==
    let sunPath = UIBezierPath(ovalInRect: CGRectMake(
      plotCenter.x - radiusX,
      plotCenter.y - radiusX,
      diameterX, diameterX)
    )

    sunPath.lineWidth = lineWidth

    edgeColor.set()
    sunPath.stroke()

    fillColor.setFill()
    sunPath.fill()

  }

  CGContextSaveGState(context)
  CGContextTranslateCTM(context,plotCenter.x ,plotCenter.y )
  CGContextRotateCTM(context, theta)
  CGContextMoveToPoint(context,0.0,0.0)
  CGContextAddLineToPoint(context, radiusX + scaleFactorDB*CGFloat(dataDB[i]) + offsetDB, 0.0 )
  myAttributedString.drawAtPoint(CGPoint(x: radius , y: 0 ))
  CGContextRestoreGState(context)

更正

  CGContextSaveGState(context)
  CGContextTranslateCTM(context,plotCenter.x ,plotCenter.y )
  CGContextRotateCTM(context, theta)
  CGContextSetStrokeColorWithColor(context,lineColor.CGColor)
  CGContextMoveToPoint(context,0.0,0.0)
  CGContextAddLineToPoint(context, radiusX + scaleFactorDB*CGFloat(dataDB[i]) + offsetDB, 0.0 )
  CGContextStrokePath(context)
  myAttributedString.drawAtPoint(CGPoint(x: radius , y: 0 ))
  CGContextRestoreGState(context)