【发布时间】:2020-09-01 13:15:44
【问题描述】:
当我覆盖 draw(_ rect: CGRect) 并将其留空时,就会调用 draw(_ layer: CALayer, in ctx: CGContext)。并且画了圆圈。
这里是代码。我刚刚分配 SpecialView 类在 IB 中查看。
class SpecialView : UIView {
override func draw(_ rect: CGRect) {
}
override func draw(_ layer: CALayer, in ctx: CGContext) {
UIGraphicsPushContext(ctx)
let p = UIBezierPath(ovalIn: CGRect(0,0,100,100))
UIColor.blue.setFill()
p.fill()
UIGraphicsPopContext()
}
}
但是当我不覆盖 draw(_ rect: CGRect) 时,只需在代码 sn-p 中保留如下代码:
class SpecialView : UIView {
override func draw(_ layer: CALayer, in ctx: CGContext) {
UIGraphicsPushContext(ctx)
let p = UIBezierPath(ovalIn: CGRect(0,0,100,100))
UIColor.blue.setFill()
p.fill()
UIGraphicsPopContext()
}
}
draw(_ layer: CALayer, in ctx: CGContext) 函数没有被调用。圆圈没有画出来。
你知道为什么吗?
【问题讨论】:
-
我觉得我以前在某个地方看过这段代码……
-
@matt 当然,在你的书中!非常感谢!
-
:) 我们不要把问题变成广告;那是错误的。
-
@matt 好的,但很抱歉我确实说过我写了这段代码。
标签: ios swift uikit core-graphics uigraphicscontext