gpt4 book ai didi

swift - 更改在特定边上带有边框的功能以也能够适应拐角半径?

转载 作者:行者123 更新时间:2023-12-03 09:29:45 34 4
gpt4 key购买 nike

所以我有以下结构和 View 扩展能够添加到任何 View ,任何想法如何修复下图中 View 的角落?

extension View {
func border(width: CGFloat, edges: [Edge], color: Color) -> some View {
overlay(EdgeBorder(width: width, edges: edges).foregroundColor(color))
}
}

struct EdgeBorder: Shape {

var width: CGFloat
var edges: [Edge]

func path(in rect: CGRect) -> Path {
var path = Path()
for edge in edges {
var x: CGFloat {
switch edge {
case .top, .bottom, .leading: return rect.minX
case .trailing: return rect.maxX - width
}
}

var y: CGFloat {
switch edge {
case .top, .leading, .trailing: return rect.minY
case .bottom: return rect.maxY - width
}
}

var w: CGFloat {
switch edge {
case .top, .bottom: return rect.width
case .leading, .trailing: return self.width
}
}

var h: CGFloat {
switch edge {
case .top, .bottom: return self.width
case .leading, .trailing: return rect.height
}
}
path.addPath(Path(CGRect(x: x, y: y, width: w, height: h)))
}
return path
}
}
我尝试将它附加到 VStack 如下:
VStack {
// content
}
.clipShape(RoundedRectangle(cornerRadius: 20))
.border(width: 1, edges: [.leading, .bottom, .trailing], color: Color.black)
唯一的问题是它最终看起来像这样:
As you can see, the bottom right corner edge doesn't conform to the clipped shape

最佳答案

我像这样用 View 环绕边界

                layerMinY
O ----------- O
layerMinX | | layerMaxX
| |
O ----------- O
layerMaxY

self.myView.layer.borderWidth = 2
self.myView.layer.borderColor = UIColor.black.cgColor
self.myView.clipsToBounds = true
self.myView.layer.cornerRadius = 10
self.myView.layer.maskedCorners = [.layerMaxXMinYCorner,
.layerMaxXMaxYCorner]
看起来像这样:
enter image description here

关于swift - 更改在特定边上带有边框的功能以也能够适应拐角半径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64860751/

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