使用坐标和ZSTACK结合视图

发布于 2025-02-04 19:37:01 字数 4507 浏览 2 评论 0原文

我在订购,结合和定位的固体形状和文本方面有一个问题

,但任务很简单,但是虽然我在swfitui中完全新手,但我不能为我的自我

task :用zstack放置坚固的形状亲戚使用坐标(包括每种形状的文本)和应用等距/怪异修饰符

我很高兴为解释和提示


使用

无修饰符

与等距修饰符一起使用

struct ContentView: View {
  @ObservedObject var datas = ReadData()
  
  //MagnificationState
    
  //DragState

  //@GestureState
  //@State viewMagnificationState
  
  //@GestureState
  //@State viewDragState
  
  //magnificationScale
  
  //translationOffset
  
  //@State popoverState
  //@State selectedShape
  
  var body: some View {
    //magnificationGesture
    
    //dragGesture
    
    //magnificationGesture.simultaneously
    
    GeometryReader { geometry in
      ZStack(alignment: .topLeading) {
        MainShape(showingPopover: $popoverState,
                  setCurrentShape: $selectedShape,
                  alldatas: datas.pokedex)
      }          
      .scaleEffect(magnificationScale)
      .offset(translationOffset)
      //.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
      //.gesture(both)
      //popover
    }
    .edgesIgnoringSafeArea(.all)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

struct MainShape: View {    
  let alldatas: [PokedexElement]

  var body: some View {
    ForEach(alldatas, id: \.id) { shape in
      
      ZStack(alignment: .topLeading) {
        Text(shape.metaProperties.name)
          .font(.system(size: 20))
          .zIndex(2)
          .offset(x: shape.metaProperties.offset.x, y: shape.metaProperties.offset.y)

        Rectangle()
          .cornerRadius(shape.id == 1 ? 55 : 10)
          .foregroundColor(chooseColor(shape.metaProperties.color))
          .offset(x: shape.metaProperties.offset.x, y: shape.metaProperties.offset.y)
          .frame(
            width: shape.metaProperties.size.width,
            height: shape.metaProperties.size.height
          )
          .isometric()
          .zIndex(1)
          .onTapGesture {
            self.showingPopover = true
            self.setCurrentShape = shape.id
          }
      }
    }
  }
}

数据:

[
    {
        "id": 1,
        "parentId": 0,
        "baseProperties": {
          "name": "",
          "descr": "",
          "contacts": ""
        },
        "metaProperties": {
            "name": "root",
            "status": true,
            "type": "plain",
            "size": {
                "width": 350,
                "height": 350
            },
            "offset": {
                "x": 0,
                "y": 0
            },
            "color": "blue"
        }
    },
    {
        "id": 2,
        "parentId": 1,
        "baseProperties": {
          "name": "object 1",
          "descr": "",
          "contacts": ""
        },
        "metaProperties": {
          "name": "child 1",
            "status": true,
            "type": "imobject",
            "size": {
                "width": 50,
                "height": 50
            },
            "offset": {
                "x": 50,
                "y": 50
            },
            "color": "red"
        }
    },
    {
        "id": 3,
        "parentId": 1,
        "baseProperties": {
          "name": "object 1",
          "descr": "",
          "contacts": ""
        },
        "metaProperties": {
          "name": "child 2",
            "status": true,
            "type": "imobject",
            "size": {
                "width": 100,
                "height": 50
            },
            "offset": {
                "x": 100,
                "y": 50
            },
            "color": "green"
        }
    }
]

修改器…

extension View {
  func chooseColor(_ name: String) -> Color {
    switch name {
    case "red":
      return Color.red
    case "blue":
      return Color.blue
    case "brown":
      return Color.brown
    case "green":
      return Color.green
    case "yellow":
      return Color.yellow
    case "white":
      return Color.white
    default :
      return Color.black
    }
  }
  
  func perspective() -> some View {
    self
      .rotation3DEffect(.degrees(45), axis: (x: 1, y: 0, z: 0))
  }
  
  func isometric() -> some View {
    self
      .rotationEffect(Angle.init(degrees: 45))
      .scaleEffect(y: 0.5)
  }
}

i have an issue with ordering, combining and positioning of my solid shapes and text

the task is simple enough, but while im completely newbie in swfitui i cannot do it for my self

task: place solid shapes with zstack relative by their parrent using coordinates (including text of each shape) and apply isometric/perpective modifier

i will be glad for explanations and hints


this is what i have done for now

works without modifiers

and no with isometric modifier

struct ContentView: View {
  @ObservedObject var datas = ReadData()
  
  //MagnificationState
    
  //DragState

  //@GestureState
  //@State viewMagnificationState
  
  //@GestureState
  //@State viewDragState
  
  //magnificationScale
  
  //translationOffset
  
  //@State popoverState
  //@State selectedShape
  
  var body: some View {
    //magnificationGesture
    
    //dragGesture
    
    //magnificationGesture.simultaneously
    
    GeometryReader { geometry in
      ZStack(alignment: .topLeading) {
        MainShape(showingPopover: $popoverState,
                  setCurrentShape: $selectedShape,
                  alldatas: datas.pokedex)
      }          
      .scaleEffect(magnificationScale)
      .offset(translationOffset)
      //.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
      //.gesture(both)
      //popover
    }
    .edgesIgnoringSafeArea(.all)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

struct MainShape: View {    
  let alldatas: [PokedexElement]

  var body: some View {
    ForEach(alldatas, id: \.id) { shape in
      
      ZStack(alignment: .topLeading) {
        Text(shape.metaProperties.name)
          .font(.system(size: 20))
          .zIndex(2)
          .offset(x: shape.metaProperties.offset.x, y: shape.metaProperties.offset.y)

        Rectangle()
          .cornerRadius(shape.id == 1 ? 55 : 10)
          .foregroundColor(chooseColor(shape.metaProperties.color))
          .offset(x: shape.metaProperties.offset.x, y: shape.metaProperties.offset.y)
          .frame(
            width: shape.metaProperties.size.width,
            height: shape.metaProperties.size.height
          )
          .isometric()
          .zIndex(1)
          .onTapGesture {
            self.showingPopover = true
            self.setCurrentShape = shape.id
          }
      }
    }
  }
}

data:

[
    {
        "id": 1,
        "parentId": 0,
        "baseProperties": {
          "name": "",
          "descr": "",
          "contacts": ""
        },
        "metaProperties": {
            "name": "root",
            "status": true,
            "type": "plain",
            "size": {
                "width": 350,
                "height": 350
            },
            "offset": {
                "x": 0,
                "y": 0
            },
            "color": "blue"
        }
    },
    {
        "id": 2,
        "parentId": 1,
        "baseProperties": {
          "name": "object 1",
          "descr": "",
          "contacts": ""
        },
        "metaProperties": {
          "name": "child 1",
            "status": true,
            "type": "imobject",
            "size": {
                "width": 50,
                "height": 50
            },
            "offset": {
                "x": 50,
                "y": 50
            },
            "color": "red"
        }
    },
    {
        "id": 3,
        "parentId": 1,
        "baseProperties": {
          "name": "object 1",
          "descr": "",
          "contacts": ""
        },
        "metaProperties": {
          "name": "child 2",
            "status": true,
            "type": "imobject",
            "size": {
                "width": 100,
                "height": 50
            },
            "offset": {
                "x": 100,
                "y": 50
            },
            "color": "green"
        }
    }
]

and modifiers…

extension View {
  func chooseColor(_ name: String) -> Color {
    switch name {
    case "red":
      return Color.red
    case "blue":
      return Color.blue
    case "brown":
      return Color.brown
    case "green":
      return Color.green
    case "yellow":
      return Color.yellow
    case "white":
      return Color.white
    default :
      return Color.black
    }
  }
  
  func perspective() -> some View {
    self
      .rotation3DEffect(.degrees(45), axis: (x: 1, y: 0, z: 0))
  }
  
  func isometric() -> some View {
    self
      .rotationEffect(Angle.init(degrees: 45))
      .scaleEffect(y: 0.5)
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2025-02-11 19:37:01

任务很容易,而不是使用“黑匣子中的魔法方法”使用常规的实体形状,所有这些都可以手动

转换为ISO,并将其与文本或其他任何内容一起放置,并且偏移会很好

task was easy, instead of using regular solid shapes with "magic methods from black box" do it all manually

convert decart to iso and place it whatever you want together with text or anything else and offset will be fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文