自定义UIView中的无效上下文0x0 [英] invalid context 0x0 within custom UIView
问题描述
对于自定义视图,我有以下代码.
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.
当我在游乐场实时取景中查看时,这看起来不错.一旦我将其投放到应用中,就会给我带来麻烦.当代码尝试执行color.setFill()和path.fill()时,在日志中出现以下错误.
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] [未知进程名称] CGContextSetFillColorWithColor:无效上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118055 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextSaveGState:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118094 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextSetFlatness:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118141 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextAddPath:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118184 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextDrawPath:无效上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118222 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextRestoreGState:无效上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118336 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextSetFillColorWithColor:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118376 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextSaveGState:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118413 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextSetFlatness:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118451 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextAddPath:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118491 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextDrawPath:无效上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量. 2018-06-01 14:37:08.118528 + 0100 SplitCircleView [21353:2290599] [未知进程名称] CGContextRestoreGState:无效上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量.
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?
推荐答案
在draw(_ rect: CGRect)
函数的文档中. (讨论"的最后一段.)
In the documentation for the draw(_ rect: CGRect)
function. (Last paragraph of the "Discussion".)
永远不要自己直接调用此方法.
You should never call this method directly yourself.
如果要触发此功能,则应使用...
If you want to trigger this function you should use...
self.setNeedsDisplay()
Documentation for the draw
function.
但是
正如@vacawama指出的(我错过了),无论如何将视图添加到视图层次结构后将调用draw函数.因此,您完全不需要在这里调用它.它会自动为您发生.
As @vacawama pointed out (and I missed) the draw function will be called after adding your view to the view hierarchy anyway. So you shouldn't need to call it at all here. It will happen automatically for you.
因此在init
方法内部,只需完全删除该调用即可.
So inside the init
methods, just remove the call altogether.
再次调用它的地方是,例如,视图是否改变形状或是否要更改其内容.即颜色或其他内容.
The place to call it again is if the view changes shape for instance or if you want to change the contents of it. i.e. colour, or something.
这篇关于自定义UIView中的无效上下文0x0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!