gpt4 book ai didi

ios - 在 UIView 周围绘制 CALayer 边框

转载 作者:行者123 更新时间:2023-12-01 16:11:49 26 4
gpt4 key购买 nike

我想在圆形 UIView 周围绘制一个带有开放间隙的边框。
我目前拥有的是这个结果:
enter image description here
我想要实现的是灰色边框位于黄色 View 之外。现在他们被画出来了,灰线的中间仍然是黄色的。
我试着戴上面具,但只剪掉了外面的部分。
我的代码:

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
}
}
}
还在评论中添加了掩码代码以显示我尝试过的内容。
测试代码:
    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)
有人可以帮我吗?

最佳答案

更改您的 circlePath .... 包括 linewidthradius将解决您的问题

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)
enter image description here

关于ios - 在 UIView 周围绘制 CALayer 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62569022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com