美文网首页
【SwiftUI】matchedGeometryEffect

【SwiftUI】matchedGeometryEffect

作者: BeethOven | 来源:发表于2024-01-28 18:15 被阅读0次

    matchedGeometryEffect iOS14的新功能 改变视图大小和位置

    struct MatchedGeometryEffectDemo: View {
        
        @State private var isExpanded = false
        @Namespace private var namespace // 命名空间
        
        var body: some View {
            ZStack {
                if isExpanded {
                    VStack {
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundColor(Color.pink)
                            .frame(width: 60, height: 60)
                            .matchedGeometryEffect(id: "rect", in: namespace)
                        Text("Hello SwiftUI!").fontWeight(.semibold)
                            .matchedGeometryEffect(id: "text", in: namespace)
                    }
                } else {
                    HStack {
                        Text("Hello SwiftUI!").fontWeight(.semibold)
                            .matchedGeometryEffect(id: "text", in: namespace)
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundColor(Color.pink)
                            .frame(width: 60, height: 60)
                            .matchedGeometryEffect(id: "rect", in: namespace)
                    }
                }
            }
            .onTapGesture {
                withAnimation {
                    isExpanded.toggle()
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:【SwiftUI】matchedGeometryEffect

          本文链接:https://www.haomeiwen.com/subject/ttnwodtx.html