swift - 我无法用弧线填充 Swift 4

标签 swift geometry fill shapes uigraphicscontext

我有一个制作圆形 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/

相关文章:

arrays - 输入新值时复制表的最后一行

C++填充矩阵 boost

geometry - 两个矩形之间的距离

ios - 在ios中绘制彩色三角形

r - 如何在R中的某些特定区域填充颜色?

CSS 填充剩余宽度空间

swift - 发现扩展时遇到的错误 : Error Domain=PlugInKit Code=13 "query cancelled"

swift - 在不触发更改的情况下更新 @Binding 值

ios - Instagram 或 Facebook 的 feed 在 iOS 中是如何工作的

colors - 在javafx上为三角形网格中的单个三角形着色