如何解决 SwiftUI 中的 MatchedGeometryEffect 淡入淡出效果问题?

How can I solve MatchedGeometryEffect fade effect issue in SwiftUI?

我在我的代码中使用 matchedGeometryEffect 来实现平滑过渡,当我使用它时,一切正常,但颜色在过程中逐渐淡出,我希望颜色不会褪色,我看起来一样问题在老问题和答案中,我使用了的方法,但我的过渡仍然有淡入淡出的效果!我怎样才能去除那里的淡入淡出效果?

struct ContentView: View {
    
    @Namespace var namespaceOfCircle
    @State private var update: Bool = false
    
    var body: some View {

        VStack {
            
            if update {
                
                VStack {
                    
                    Circle()
                        .foregroundColor(Color.black)
                        .matchedGeometryEffect(id: "Circle", in: namespaceOfCircle)
                        .transition(.scale(scale: 1))
                        .frame(width: 100, height: 100)
                    
                    Spacer()

                }
                
            }
            else {
                
                VStack {
                    
                    Spacer()
                    
                    Circle()
                        .foregroundColor(Color.black)
                        .matchedGeometryEffect(id: "Circle", in: namespaceOfCircle)
                        .transition(.scale(scale: 1))
                        .frame(width: 300, height: 300)

                }
                
            }
            
        }
        .background(Button("update") { update.toggle() })
        .animation(.linear(duration: 5.0), value: update)
  
    }
}

应用修饰符的地方很重要,这里你 insert/remove 不是圆形而是 VStack,所以解决方法是将过渡移动到那里。

测试 Xcode 13.3 / iOS 15.4

主要部分是:

if update {
    VStack {
        Circle()
            .matchedGeometryEffect(id: "Circle", in: namespaceOfCircle)
            .frame(width: 100, height: 100)
        Spacer()
    }
    .transition(.scale(scale: 1))      // << here !!

完成findings and code is here