Transitions on inner Views when replacing an outer View altogether

Starting from this example: Two views in a VStack, coming and going with a different transition:

struct ContentView: View {
    @State var value = false
    
    var body: some View {
        VStack {
            ZStack {
                value ? Color.red : Color.green
                
                VStack {
                    Text(String(describing: value))
                    Button("Toggle") {
                        value.toggle()
                    }
                }
            }
            .id(value)
            .transition(.move(edge: .leading))
            
            ZStack {
                value ? Color.yellow : Color.orange
                Text(String(value))
            }
            .frame(height: 200)
            .id(value)
            .transition(.move(edge: .bottom))
        }
        .animation(.easeInOut, value: self.value)
    }
}

This works fine. Now, what surprised me is this: When you move out the .id-modifier (“replace the outer view”), the inner views don’t do the transitions any more:

struct ContentView: View {
    @State var value = false
    
    var body: some View {
        VStack {
            ZStack {
                value ? Color.red : Color.green
                
                VStack {
                    Text(String(describing: value))
                    Button("Change") {
                        value.toggle()
                    }
                }
            }
            // .id(value)   <-- not here
            .transition(.move(edge: .leading))
            
            ZStack {
                value ? Color.yellow : Color.orange
                Text(String(value))
            }
            .frame(height: 200)
            // .id(value)   <-- not here
            .transition(.move(edge: .bottom))
        }
        .id(value)   // <-- moved here
        .animation(.easeInOut, value: self.value)
    }
}

This seems to be how transitions work in the SwiftUI, it seems the transition must go on the View node that’s replaced. If a View with a transition comes and goes because its parent View comes and goes, this doesn’t trigger the transition.

To clarify the example, here is the same example without using .id() where a container view wants parts of its content to come and go with a transition:

struct View1: View {
    @Binding var value: Bool
    var body: some View {
        VStack {
            ZStack {
                value ? Color.red : Color.green
                
                VStack {
                    Text(String(describing: value))
                    Button("Change") {
                        value.toggle()
                    }
                }
            }
            .transition(.move(edge: .leading))

            ZStack {
                value ? Color.yellow : Color.orange
                Text(String(value))
            }
            .frame(height: 200)
            .transition(.move(edge: .bottom))
        }
    }
}

struct View2: View {
    @Binding var value: Bool
    var body: some View {
        Button("Change") {
            value.toggle()
        }
    }
}


struct ContentView: View {
    @State var value = false
    
    var body: some View {
        ZStack {
         
            if value {
                View1(value: $value)
            } else {
                View2(value: $value)
            }
            
        }
        .animation(.easeInOut, value: self.value)
    }
}

Question:
Is it possible to add transitions to inner views in SwiftUI that are triggered by replacing the outer View? (as in: can the last example have transitions while keeping View1/View2 separate/without being sneaky with the View hierarchy?)

1

You wrote:

it seems the transition must go on the View node that’s replaced

This is correct.

The behavior in your examples can be explained as follows:

  • In the first version, the .transition modifier was on the container (the ZStack). The transitions were working because the id of the container changed whenever value changed. This made it seem like a different container every time.

  • In the second version, the id remains the same, so even though the contents of the container is changing, the container itself is the same. This is why there is no transition happening for the container.

For your re-factored version using View1 and View2, you can get it to work with these changes:

  • In View1, move the .transition modifier from the container to the actual colors, since these are the views that are actually changing.

  • It seems it doesn’t work when switching views using a ternary operator, so use an if-else switch instead.

  • You also need to toggle the flag withAnimation, or add an .animation modifier to a containing view.

struct View1: View {
    @Binding var value: Bool
    var body: some View {
        VStack {
            ZStack {
                if value {
                    Color.red
                        .transition(.move(edge: .leading))
                } else {
                    Color.green
                        .transition(.move(edge: .leading))
                }
                VStack {
                    Text(String(describing: value))
                    Button("Change") {
                        value.toggle()
                    }
                }
            }

            ZStack {
                if value {
                    Color.yellow
                        .transition(.move(edge: .bottom))
                } else {
                    Color.orange
                        .transition(.move(edge: .bottom))
                }
                Text(String(value))
            }
            .frame(height: 200)
        }
        .animation(.easeInOut, value: value) // 👈 ADDED
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật