I've stripped this issue down to an image on a ZStack. The image should be in the lower left corner of the ZStack. When the .rotation for the image is modified, the location changes as if the underlying ZStack was rotated.
Here's the code:
import SwiftUI
let gridSize: Int = 20
let cellSize: Int = 30
struct MazeView: View {
// what cell is the player in -- start lower left
@State var playerCell: (Int, Int)? = (gridSize - 1, 0)
var body: some View {
ZStack {
// player icon
Image("Character center")
.resizable()
.frame(width: CGFloat(cellSize - 2), height: CGFloat(cellSize - 2))
.position(playerPosition())
.rotationEffect(.degrees(180)) // <- change this to mults of 90
} // ZStack - the maze
.frame(width: CGFloat(gridSize * cellSize), height: CGFloat(gridSize * cellSize))
.border(Color.black)
} // body
private func playerPosition() -> CGPoint {
if let current = playerCell {
return CGPoint(x: (cellSize * current.1) + cellSize/2,
y: (cellSize * current.0) + cellSize/2)
} else {
return CGPoint(x: cellSize/2, y: gridSize * cellSize + cellSize/2)
}
}
}
}
.rotationEffectis being applied after.position. Try changing the order of these two modifiers.