Hello everyone!
In the Moving views with DragGesture and offset() section there’s a part on the importance of modifier order, esspesially when dealling with offsets and rotations:
If we rotate then offset, then the offset is applied based on the rotated axis of our view. For example, if we move something 100 pixels to its left then rotate 90 degrees, we’d end up with it being 100 pixels to the left and rotated 90 degrees. But if we rotated 90 degrees then moved it 100 pixels to its left, we’d end up with something rotated 90 degrees and moved 100 pixels directly down, because its concept of “left” got rotated.
Where things get doubly tricky is when you factor in how SwiftUI creates new views by wrapping modifiers. When it comes to moving and rotating, this means if we want a view to slide directly to true west (regardless of its rotation) while also rotating it, we need to put the rotation first then the offset.
Initially, I assumed the way to move the card view directly to the west (or east) would be by applying offset first and then rotationEffect, and the first paragraph above seemed to reinforce that idea. However, it turns out the correct order is to apply rotationEffect first, then offset, which i find a bit puzzling.
Even more baffling, applying offset first and then rotationEffect makes the card view move in an arc directi.
No mater how many times i reread the second paragraph, i still can't understand why the correct order is rotation first, and offset second.
Could anyone help clarify why the rotation needs to come first here?