I have the following code for a custom view.
@IBDesignable class SplitCircleView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
draw(frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
draw(frame)
}
override func draw(_ rect: CGRect) {
self.backgroundColor = .clear
drawSlice(rect: self.frame, startPercent: 87.5, endPercent: 37.5, color: .green)
drawSlice(rect: self.frame, startPercent: 37.5, endPercent: 87.5, color: .red)
}
func drawSlice(rect: CGRect, startPercent: CGFloat, endPercent: CGFloat, color: UIColor) {
let center = CGPoint(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height / 2)
let radius = (min(rect.width, rect.height) / 2)
let startAngle = startPercent / 100 * .pi * 2 - .pi
let endAngle = endPercent / 100 * .pi * 2 - .pi
let path = UIBezierPath()
path.move(to: center)
path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.close()
color.setFill()
path.fill()
}
}
I'm trying to draw a circle with two semi circles different colors.
This looks fine when I view it in a playground live view. Once I put it an app it causes me problems. When the code tries to execute color.setFill() and path.fill() I get the following errors in the log.
2018-06-01 14:37:08.118005+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118055+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118094+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118141+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118184+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118222+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118336+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118376+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118413+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118451+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118491+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118528+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
What am I doing wrong and how can I fix this?