I am trying to create a grid within SwiftUI where the index "snakes" from left-to-right, right-to-left, and left-to-right (ad infinitum). The approach that I've been trying to implement is essentially applying rotationEffect
to each row, but the LazyVGrid
's GridItem
s cannot handle a rotation effect.
An example of how I am looking to lay out the array
Providing some rough code for my View, but I am having trouble figuring out if there is a way of extending GridItem or applying a row-based rotationEffect without implementing a bunch of separate ForEach statements that rely on index checks (which seems inefficient, especially as I am using a single array and would need to drag and drop between them.
struct ContentView: View {
@StateObject private var game = GameDriver()
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
var body: some View {
LazyVGrid(columns: columns, spacing: 8) {
ForEach(Array(game.hand.enumerated()), id: \.offset) { index, tile in
CardView(game: self.game, card: card)
}
}
}
I have tried to implement a rotationEffect on GridItem, but Type Grid Item does not have rotationEffect as a member, so I cannot affect the whole row. I have also tried implementing the rotationEffect within ForEach, but as expected, that only results in an individual card being flipped upside down, as opposed to the whole row as desired.