我有一个制作圆形 UIView 的类。当我将圆圈添加到我的 View 时,它显示红色轮廓,但内部是清晰的而不是黑色。我希望它有黑色填充。
class CircleView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
context.setLineWidth(1.0);
UIColor.red.set()
let center = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
let radius = (frame.size.width - 10)/2
context.addArc(center: center, radius: radius, startAngle: 0.0, endAngle: .pi * 2.0, clockwise: true)
context.strokePath()
context.closePath()
context.setFillColor(UIColor.black.cgColor)
context.fillPath()
}
}
}
最佳答案
使用 UIBezierPath
可以更轻松地解决这个问题。
override func draw(_ rect: CGRect) {
let path = UIBezierPath(ovalIn: bounds)
path.lineWidth = 1
UIColor.red.setStroke()
UIColor.black.setFill()
path.fill()
path.stroke()
}
关于swift - 我无法用弧线填充 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53014002/