简体   繁体   English

在 UIView 周围绘制 CALayer 边框

[英]Draw CALayer border around UIView

I want to draw a border with open gaps around a rounded UIView.我想在圆形 UIView 周围画一个带有开放间隙的边框。

What I currently have is this result:我目前拥有的是这个结果:

在此处输入图像描述

What I want to achieve is that the gray borders are laying outside the yellow view.我想要实现的是灰色边框位于黄色视图之外。 Now they are drawn that the middle of the gray line is still in the yellow.现在他们被绘制出来,灰线的中间仍然是黄色。

I tried with a mask but then only the oudside ofcourse is cut.我试着戴上面具,但只剪掉了 oudside ofcourse。

My code:我的代码:

struct Config {
    let start: CGFloat
    let end: CGFloat
    let color: UIColor
}

extension UIView {
    func drawBorders(for configs: [Config], lineWidth: CGFloat = 3.5) {
        let circlePath = UIBezierPath(arcCenter: CGPoint (x: self.bounds.size.width / 2,
                                                          y: self.bounds.size.height / 2),
                                      radius: self.bounds.size.width / 2,
                                      startAngle: CGFloat(-0.5 * Double.pi),
                                      endAngle: CGFloat(1.5 * Double.pi),
                                      clockwise: true)

        for config in configs {
            let circleShape = CAShapeLayer()
            circleShape.path = circlePath.cgPath
            circleShape.strokeColor = config.color.cgColor
            circleShape.fillColor = UIColor.clear.cgColor
            circleShape.lineWidth = lineWidth
            circleShape.strokeStart = config.start
            circleShape.strokeEnd = config.end

            self.layer.addSublayer(circleShape)

//            let maskLayer = CAShapeLayer()
//                 maskLayer.frame = self.bounds
//            maskLayer.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: self.bounds.size.width / 2, height: self.bounds.size.width / 2)).cgPath
//                 self.layer.mask = maskLayer
        }
    }
}

Also added the mask code in comment to show what I have tried.还在评论中添加了掩码代码以显示我尝试过的内容。

Test code:测试代码:

    let roundView = UIView(frame: CGRect(x: 100, y: 100, width: 150, height: 150))
    roundView.backgroundColor = .yellow
    roundView.layer.cornerRadius = roundView.frame.size.width / 2

    let config1 = Config(start: 0.125, end: 0.25, color: .gray)
    let config2 = Config(start: 0.375, end: 0.5, color: .gray)
    let config3 = Config(start: 0.625, end: 0.75, color: .gray)
    let config4 = Config(start: 0.875, end: 1, color: .gray)

    roundView.drawBorders(for: [config1, config2, config3, config4])
    view.addSubview(roundView)

Can someone please help me out?有人可以帮我吗?

Change your circlePath .... include linewidth in radius will resolve your issue更改您的circlePath .... 在radius中包含linewidth将解决您的问题

let circlePath = UIBezierPath(arcCenter: CGPoint (x: self.bounds.size.width / 2,
                                                          y: self.bounds.size.height / 2),
                                      radius: (self.bounds.size.width / 2) + lineWidth/2,
                                      startAngle: CGFloat(-0.5 * Double.pi),
                                      endAngle: CGFloat(1.5 * Double.pi),
                                      clockwise: true)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM