我试图在Xcode 8,Swift 3上的UIView子类中画一个蓝色圆圈。在我作为Storyboard中一个视图对象的类使用的Cocoa文件中,我编写了以下代码,但是没有显示这个圆圈:
import UIKit
class testView: UIView {
override func draw(_ layer: CALayer, in ctx: CGContext) {
ctx.addEllipse(in: CGRect(x: 0, y: 0, width: 100, height: 100))
ctx.setFillColor(UIColor.blue.cgColor)
ctx.fillPath()
}
}这个带有UIKit的版本也不会显示圆圈:
override func draw(_ layer: CALayer, in ctx: CGContext) {
UIGraphicsPushContext(ctx)
let p = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 100))
UIColor.blue.setFill()
p.fill()
UIGraphicsPopContext()
}我做错了什么?
发布于 2016-10-11 15:13:16
我正在研究你的问题,这是我的结果,首先你需要给你CurrentGraphicsContent,如果你不这样做,你什么都画不出来,所以试试下面的代码
override func draw(_ rect: CGRect) {
super.draw(rect)
if let ctx = UIGraphicsGetCurrentContext() {
ctx.setFillColor(UIColor.blue.cgColor)
ctx.addEllipse(in: CGRect(x: 0, y: 0, width: 100, height: 100))
ctx.fillPath()
UIGraphicsEndImageContext()
}
// Drawing code
}我希望这对你有帮助
https://stackoverflow.com/questions/39968800
复制相似问题