Swiftui 是否有方法将路径添加到已渲染的路径
TL;DR-我想添加一个Swiftui 是否有方法将路径添加到已渲染的路径,swiftui,Swiftui,TL;DR-我想添加一个路径或形状?我想知道我是否可以使用path.addPath(…)查看图片以了解我的意思 早些时候我发布了一个关于这个的问题,但我不认为我的措辞很好,很可能是走错了方向。我想做的就是将形状添加到现有形状中。我看到Path有一个方法addPath(:),可以让我们将一条路径添加到另一条路径。。如果我能想出在哪里做这件事,那就太完美了。以下是我目前的尝试: import SwiftUI struct ContentView: View { @State privat
路径
或形状
?我想知道我是否可以使用path.addPath(…)
查看图片以了解我的意思
早些时候我发布了一个关于这个的问题,但我不认为我的措辞很好,很可能是走错了方向。我想做的就是将形状添加到现有形状中。我看到Path
有一个方法addPath(:)
,可以让我们将一条路径添加到另一条路径。。如果我能想出在哪里做这件事,那就太完美了。以下是我目前的尝试:
import SwiftUI
struct ContentView: View {
@State private var path = Path()
var body: some View {
VStack {
MyShape()
.stroke(lineWidth: 3)
.padding()
HStack {
AddHorizontalLineView()
AddVerticalLineView()
AddSquareView()
AddDiagonalLineView()
}
}
}
private func addBoxToPath(_ path: Path) -> Path { <-- No where to call this
var workingPath = path
workingPath.addRect(workingPath.boundingRect)
return workingPath
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct MyShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.addRect(rect)
return path
}
}
struct AddHorizontalLineView: View {
var body: some View {
Button(action: {}){
Rectangle()
.stroke()
.overlay(
HorizontalWire()
.stroke()
.padding()
)
}.frame(width: 50, height: 50)
}
}
struct AddVerticalLineView: View {
var body: some View {
Button(action: { }){
Rectangle()
.stroke()
.overlay(
VerticalWire()
.stroke()
.padding()
)
}.frame(width: 50, height: 50)
}
}
struct AddSquareView: View {
var body: some View {
Button(action: {
}){
Rectangle()
.stroke()
.overlay(
Rectangle()
.stroke()
.padding()
)
}.frame(width: 50, height: 50)
}
}
struct AddDiagonalLineView: View {
var body: some View {
Button(action: {}){
Rectangle()
.stroke()
.overlay(
DiagonalWire()
.stroke()
.foregroundColor(.red)
.padding()
)
}
.frame(width: 50, height: 50)
}
}
struct DiagonalWire: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.minX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
return path
}
}
struct VerticalWire: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.midX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
return path
}
}
struct HorizontalWire: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.minX, y: rect.midY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
return path
}
}
导入快捷界面
结构ContentView:View{
@状态私有变量路径=路径()
var body:一些观点{
VStack{
MyShape()
.笔划(线宽:3)
.padding()
HStack{
AddHorizontalLineView()
AddVerticalLineView()
AddSquareView()
添加对角线视图()
}
}
}
private func addBoxToPath(u路径:path)->path{path{
var path=path()
path.addRect(rect)
返回路径
}
}
结构AddHorizontalLineView:视图{
var body:一些观点{
按钮(操作:{}){
矩形()
.中风(
.覆盖(
水平线()
.中风(
.padding()
)
}.框架(宽度:50,高度:50)
}
}
结构AddVerticalLineView:视图{
var body:一些观点{
按钮(操作:{}){
矩形()
.中风(
.覆盖(
垂直线()
.中风(
.padding()
)
}.框架(宽度:50,高度:50)
}
}
结构AddSquareView:视图{
var body:一些观点{
按钮(操作:{
}){
矩形()
.中风(
.覆盖(
矩形()
.中风(
.padding()
)
}.框架(宽度:50,高度:50)
}
}
结构添加对角线视图:视图{
var body:一些观点{
按钮(操作:{}){
矩形()
.中风(
.覆盖(
斜线
.中风(
.foregroundColor(.red)
.padding()
)
}
.框架(宽度:50,高度:50)
}
}
结构对角线:形状{
func路径(在rect:CGRect中)->path{
var path=path()
移动(到:CGPoint(x:rect.minX,y:rect.minY))
addLine(到:CGPoint(x:rect.maxX,y:rect.maxY))
返回路径
}
}
结构垂直线:形状{
func路径(在rect:CGRect中)->path{
var path=path()
移动路径(到:CGPoint(x:rect.midX,y:rect.minY))
addLine(到:CGPoint(x:rect.midX,y:rect.maxY))
返回路径
}
}
结构水平线:形状{
func路径(在rect:CGRect中)->path{
var path=path()
移动路径(到:CGPoint(x:rect.minX,y:rect.midY))
addLine(到:CGPoint(x:rect.maxX,y:rect.midY))
返回路径
}
}
是否可以像这样动态添加路径,或者需要在符合Shape的每种类型的path(in:)
方法中预先完成整个设置
我希望我的问题现在更有意义。您可以将路径
传递到我的形状
(尽管它当时真的没有什么意义——您可以只渲染路径本身)
下面是一个简单的例子(删除了一些按钮,因为连接所有逻辑需要大量的工作)。我还必须在路径
中添加一个额外的rect
,因为您的addBoxToPath
取决于路径
中是否存在某些内容,因此边界框
不仅仅是一个0x0
rect
:
struct ContentView: View {
@State private var path = Path()
var body: some View {
VStack {
MyShape(path: path)
.stroke(lineWidth: 3)
.padding()
HStack {
Button(action: {
path = addBoxToPath(path)
}) {
Text("add")
}
}
}.onAppear {
path.addRect(CGRect(origin: .zero, size: CGSize(width: 200, height: 200)))
}
}
private func addBoxToPath(_ path: Path) -> Path {
var workingPath = path
workingPath.addRect(workingPath.boundingRect.insetBy(dx: -5, dy: -5))
return workingPath
}
}
struct MyShape: Shape {
var path : Path
func path(in rect: CGRect) -> Path {
return path
}
}