0

I have 4 similar views, like SettingsPointOneView() for each point in figure. They store 2 views SliderFirstPointX() and SliderFirstPointY() in this case. They (SliderFirstPointX etc...)control and change values of axis(x,y) for particular point of custom figure, with sliders.

How it looks:

enter image description here

SliderFirstPointX() and SliderFirstPointY()

struct SliderFirstPointX: View{
@EnvironmentObject var geometryBuilder : GeometryBuilderViewModel
var body: some View{
    VStack{
        Text("Point one: X")
        Text("\(Int(geometryBuilder[0].x))")
            .foregroundColor(.gray)
            .opacity(0.5)
        Slider(value: $geometryBuilder[0].x, in: 10...380)
            .accentColor(.orange)
            .padding(3)
    }
}
}


struct SliderFirstPointY: View{
@EnvironmentObject var geometryBuilder: GeometryBuilderViewModel
var body: some View{
    VStack{
        Text("Point one: Y")
        Text("\(Int(geometryBuilder[0].y))")
            .foregroundColor(.gray)
            .opacity(0.5)
        Slider(value: $geometryBuilder[0].y, in: 0...380)
            .accentColor(.orange)
            .padding(3)
    }
}
}

SettingsPointOneView()

struct SettingsPointOneView: View{
var body: some View{
    ZStack{
        RoundedRectangle(cornerRadius: 10)
            .stroke()
            .frame(width: 360, height: 150, alignment: .leading)

        HStack{
            SliderFirstPointX()
            SliderFirstPointY()
        }.padding([.leading, .trailing], 50)
    }
}
}

I'd wanted to position them into LazyVGrid, similar how it is on screenshot. So I've created constant var and tried to initialize it like:

let arrayOfViews: [Any] = [SettingsPointOneView(), SettingsPointTwoView(), SettingsPointThreeView(), SettingsPointFoutView()]

After that, several error messages occurred, that I don't how to work with:

In front of ForEach: • Protocol 'Any' as a type cannot conform to 'AccessibilityRotorContent

In front of LazyVGrid:

• Generic struct 'LazyVGrid' requires that 'some AccessibilityRotorContent' conform to 'View';

• Initializer 'init(columns:alignment:spacing:pinnedViews:content:)' requires that 'some AccessibilityRotorContent' conform to 'View';

• Struct 'ViewBuilder' requires that 'some AccessibilityRotorContent' conform to 'View'

Because I hadn't found remedy, I had to use this implementation, but it's not satisfy me:

struct ContentView: View {

...some code...

let first = [SettingsPointOneView()]
let second = [SettingsPointTwoView()]
let third = [SettingsPointThreeView()]
let fourth = [SettingsPointFourView()]
     var body: some View {

      ..some code...

       ScrollView{
            LazyVGrid(columns: [GridItem(.flexible(minimum: 100, maximum: 200))]){
                ForEach(0...0, id: \.self){
                    first[$0].padding(.top)
                    second[$0]
                    third[$0]
                    fourth[$0]
                }
        }

..some code...

       }

This solution works pretty well, although I guess this is wrong way to implement this feature.

Is there any way to use approach with array of Views in ForEach?

Truth be told, I've been programming for 3 months, so I'll appreciate any advice about what I should learn as Swift developer.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Suprafen
  • 58
  • 3
  • 9
  • Why do you need to use a `ForEach`? That's really overcomplicating things. Just use the views there directly, it's static after all. – George Aug 19 '21 at 10:38
  • @George, you're right, I can just put views into ScrollView. By the way I want to understand, why I'm not able to use array of views in ForEach. – Suprafen Aug 19 '21 at 11:25
  • `ForEach` is used for repeating the same view, maybe taking in different parameters. It's not intended for different types of views. – George Aug 19 '21 at 11:27
  • Oh, thank you for that, I should have to read more about it. @George – Suprafen Aug 19 '21 at 11:36

0 Answers0