The following SwiftUI view demonstrates an animation glitch that seems to be caused by ViewThatFits
. To see the issue, render the following view and tap on the red section. To see that it works without the ViewThatFits
tap on the blue section. To be specific, the glitch that I am observing is that the purple view snaps instantly into its new position on the screen without respecting the animated transition, while the green view performs the transition as expected. This glitch does not occur if the purple view is not wrapped inside of ViewThatFits
.
///
struct ViewThatFitsAnimationGlitchDemo: View {
///
@State var displayedSubpage: Subpage? = nil
///
enum Subpage {
case normal
case glitchy
}
///
var body: some View {
///
VStack(spacing: 0) {
tappableColor(.blue) {
withAnimation {
displayedSubpage = .normal
}
}
tappableColor(.red) {
withAnimation {
displayedSubpage = .glitchy
}
}
}
.overlay {
if let displayedSubpage {
switch displayedSubpage {
case .normal:
normalSubpage
.transition(.move(edge: .trailing))
case .glitchy:
glitchySubpage
.transition(.move(edge: .trailing))
}
}
}
}
///
var normalSubpage: some View {
VStack(spacing: 0) {
///
Color.purple
///
tappableColor(.green) {
withAnimation {
displayedSubpage = nil
}
}
}
}
///
var glitchySubpage: some View {
VStack(spacing: 0) {
///
ViewThatFits {
Color.purple
}
///
tappableColor(.green) {
withAnimation {
displayedSubpage = nil
}
}
}
}
///
func tappableColor
(_ color: Color,
onPressed: @escaping ()->())
-> some View {
///
Button(
action: onPressed,
label: { color }
)
.buttonStyle(.plain)
}
}