SwiftUI - Почему размещение модификатора .animation () так трогательно? - PullRequest
0 голосов
/ 01 апреля 2020

TL; DR

Почему модификатор animation() необходимо присоединять к группировке, подобной ZStack, а не непосредственно к объекту, который он изменяет?

Примечания

Это все в коде.

Код

struct ContentView: View {
    @State private var showRects = false

    var body: some View {
        VStack {

            // I seem to need a dummy ZStack.
            ZStack {
                if showRects {
                    Rectangle().foregroundColor(.blue)
                }
            }.animation(.easeInOut(duration:1)) // Animation plays.

            // But a dummy Group doesn't work.
            Group {
                if showRects {
                    Rectangle().foregroundColor(.gray)
                }
            }.animation(.easeInOut(duration:1))  // Does not play.

            // I thought this was plausible...why not?
            if showRects {
                Rectangle().foregroundColor(.green)
                .animation(.easeInOut(duration: 1)) // Does not play.
            }

            // Invert the nesting? No go.
            if showRects {
                ZStack {
                    Rectangle().foregroundColor(.red)
                }.animation(.easeInOut(duration: 1)) // Does not play.
            }

            Button(action: { self.showRects.toggle() } ) {
                Text("Toggle Me").font(.system(size: 32))
            }

        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...