gpt4 book ai didi

swift - 如何在 SwiftUI 中绘制自定义形状?

转载 作者:行者123 更新时间:2023-12-04 08:27:04 25 4
gpt4 key购买 nike

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

9 个月前关闭。




Improve this question




亲爱的 IOS 开发者,您好。你能帮我画几何形状吗?
enter image description here

最佳答案

您可以通过使用 Path 和 GeometryReader 来实现:

struct ContentView : View {
var body: some View {
Text("Breakfast")
.foregroundColor(.white)
.font(.title)
.padding(20)
.background(RoundedCorners(color: .green, topLeft: 10, topRight: 5, bottomLeft: 0, bottomRight: 30))
}
}
struct RoundedCorners: View {
var color: Color
var topLeft: CGFloat = 0.0
var topRight: CGFloat = 0.0
var bottomLeft: CGFloat = 0.0
var bottomRight: CGFloat = 0.0

var body: some View {
GeometryReader { geometry in
Path { path in

let w = geometry.size.width
let h = geometry.size.height

let tr = min(min(self.topRight, h/2), w/2)
let tl = min(min(self.topLeft, h/2), w/2)
let bl = min(min(self.bottomLeft, h/2), w/2)
let br = min(min(self.bottomRight, h/2), w/2)

path.move(to: CGPoint(x: w / 2.0, y: 0))
path.addLine(to: CGPoint(x: w - tr, y: 0))
path.addArc(center: CGPoint(x: w - tr, y: tr), radius: tr, startAngle: Angle(degrees: -90), endAngle: Angle(degrees: 0), clockwise: false)
path.addLine(to: CGPoint(x: w, y: h - br))
path.addArc(center: CGPoint(x: w - br, y: h - br), radius: br, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 90), clockwise: false)
path.addLine(to: CGPoint(x: bl, y: h))
path.addArc(center: CGPoint(x: bl, y: h - bl), radius: bl, startAngle: Angle(degrees: 90), endAngle: Angle(degrees: 180), clockwise: false)
path.addLine(to: CGPoint(x: 0, y: tl))
path.addArc(center: CGPoint(x: tl, y: tl), radius: tl, startAngle: Angle(degrees: 180), endAngle: Angle(degrees: 270), clockwise: false)
}
.fill(self.color)
}
}
}
结果:
enter image description here
您可以找到更多信息 here .

关于swift - 如何在 SwiftUI 中绘制自定义形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65207604/

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