0

I'm trying to create a stack of poker chips in SwiftUI. I've got a ChipView which is a view of a single chip. I'm now trying to layer ChipViews in a stack to create of poker chips. I'm having trouble building a for loop following the rules of a view builder.

I want to do the following:

    ZStack {
        for index in 0..<chipCount {
            ChipView()
                .offset( CGSize(width: index * 5, height: index * 5))
        }
    }

But I know that I can't do that in a view builder. I know I should use ForEach but can't think of how. I could build an array of indexes to use ForEach(indexArray) { index in ... } but that seems very clunky and unsatisfying.

I feel like this is trivially easy but I couldn't a solution from googling.

user1357607
  • 214
  • 4
  • 13

1 Answers1

0

So...it turns out, that is is trivially easy. I found the answer to my question right here: How to have a dynamic List of Views using SwiftUI

My code now looks like this:

ZStack{
    ForEach (0..<chipCount) { index in
         ChipView()
              .offset( CGSize(width: index * 5, height: index * 5))

    }
}
user1357607
  • 214
  • 4
  • 13