私は曲線を描くプログラムを書こうとしています。カーブが短いほど、緑に近づきます。ここで円を描くためのコードは次のとおりです。スロー - 曲線を描くときのエラー
func drawCircle(percent: CGFloat) {
// Set up all the data for the circle
let redAmt = 1/100 * percent
let greenAmt = 1 - redAmt
let circleColor = UIColor.init(red: redAmt, green: greenAmt, blue: 0, alpha: 1)
let lineWidth = CGFloat(12)
let centerX = self.frame.size.width/2
let centerY = self.frame.size.height/2
let radius = (self.frame.size.width - lineWidth)/2
// Clear the area where the circle is going to be drawn
let clearCirclePath = UIBezierPath(arcCenter: CGPoint(x: centerX, y: centerY), radius: radius, startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2), clockwise: true)
clearColor.setStroke()
clearCirclePath.lineWidth = lineWidth
clearCirclePath.stroke()
// Draw the circle
let circlePath = UIBezierPath(arcCenter: CGPoint(x: centerX, y: centerY), radius: radius, startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2) * percent/100, clockwise: true)
circleColor.setStroke()
circlePath.lineWidth = lineWidth
circlePath.stroke()
}
drawRect(rect: CGRect)
によって呼び出されたとき、それはうまく動作しますが、私は別のファイルで呼び出すようにしようとすると、それは動作しません。ビューコントローラ内のビュー内に弧を描画しています。それが私に与えるエラーは次のとおりです:ExerciseTracker[7270] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
ありがとう!
(私はXcode 7.3.1を使用しています)