我在SwiftUI中创建了一个表单。有一个地方我绕不开。你能帮忙吗?
这是我的代码:
struct BannerShape: Shape {
func path(in rect: CGRect) -> Path {
return Path { path in
path.move(to: .zero)
path.addLine(to: CGPoint(x: 0, y: 100))
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 100))
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 0))
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width / 2 + 48, y: 0))
path.addCurve(to: CGPoint(x: UIScreen.main.bounds.width / 2 - 48, y: 0),
control1: CGPoint(x: UIScreen.main.bounds.width / 2 + 80, y: 105),
control2: CGPoint(x: UIScreen.main.bounds.width / 2 - 80, y: 105))
path.addLine(to: .zero)
path.closeSubpath()
}
}
}
在我的预览中:
struct TabBarBackgroundView_Previews: PreviewProvider {
// @State static var selectedTabPreview: Int = 0
static var previews: some View {
// TabBarBackgroundView(selectedTab: $selectedTabPreview)
BannerShape()
.cornerRadius(15, corners: [.topLeft, .topRight])
.previewLayout(.sizeThatFits)
.frame(width: UIScreen.main.bounds.width, height: 100)
.padding()
}
}
结果:
我想把圆附近的两个角弄圆,因为它形成一个点。
我还没找到能做这个的东西。
我就是这么试的:
struct MyShape : Shape {
func path(in rect: CGRect) -> Path {
return Path { path in
path.move(to: .zero)
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width / 2 - 48, y: 0))
path.addArc(center: CGPoint(x: UIScreen.main.bounds.width / 2 - 48, y: 10),
radius: 10,
startAngle: .degrees(-90),
endAngle: .degrees(0),
clockwise: false)
path.addCurve(to: CGPoint(x: UIScreen.main.bounds.width / 2 + 48, y: 0),
control1: CGPoint(x: UIScreen.main.bounds.width / 2 - 80, y: 105),
control2: CGPoint(x: UIScreen.main.bounds.width / 2 + 80, y: 105))
path.addArc(center: CGPoint(x: UIScreen.main.bounds.width / 2 + 48, y: 10),
radius: 10,
startAngle: .degrees(-90),
endAngle: .degrees(-180),
clockwise: true)
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 0))
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: 100))
path.addLine(to: CGPoint(x: 0, y: 100))
path.addLine(to: .zero)
path.closeSubpath()
}
}
}
结果呢...
1条答案
按热度按时间gev0vcfq1#
Soooo close:)
第一个
.addArc
的center.x
位于:你把第二个
.addArc
的center.x
放在:这是合乎逻辑的
但是,当添加半径为
10
的第一个圆弧时,四曲线x
将从10-points
开始(向右)...所以,你需要把四曲线10-points
短(向左):而且,你的第二个
.addArc
也需要clockwise: false
(我认为SwiftUI顺时针很古怪,但你可以在这里找到解释:https://stackoverflow.com/a/57226474/6257435)下面是经过这些修改的代码:
我们得到这个:
如果你使用
.addArc(tangent1End:tangent2End:radius:)
,你可以得到更平滑的曲线:这给了我们这个结果: