0

我是 swiftUI 的新手。我想要这样的浮动标签的TextFeild。我用matchedGeometryEffect() 试过了,但还不够。不知道是什么毛病。它在没有动画的情况下完美运行。

在此处输入图像描述

当我添加动画时,情况会变得更糟

在此处输入图像描述

这是我的代码

struct TextFeildView: View {
    
    private var placeHolder: String = ""
    @Binding private var text: String
    @Namespace private var titleNameSpace
    
    init(_ placeHolder: String, text: Binding<String> ) {
        self.placeHolder = placeHolder
        self._text = text
    }
    
    var body: some View {
        VStack() {
            HStack {
                Text(placeHolder)
                    .hidden(text == "")
                    .animation(.default)
                    .matchedGeometryEffect(id: "titleNameSpace", in: titleNameSpace)
                Spacer()
            }.padding(.horizontal,32)
            VStack {
                ZStack(alignment: .leading) {
                    Text(placeHolder)
                        .hidden(text != "")
                        .animation(.default)
                        .matchedGeometryEffect(id: "titleNameSpace", in: titleNameSpace)
                    TextField("", text: $text)
                }
                Color.gray
                    .frame(width: screenFrame.Width - 64, height: 1, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
            }.padding(.horizontal,32)
        }
    }
}

struct TextFeildView_orignal: View {
    @State var text : String = ""
    var body: some View {
        TextFeildView("PlaceHolder", text: $text)
    }
}

struct TextFeildView_Previews: PreviewProvider {
    static var previews: some View {
        TextFeildView_orignal()
    }
}

请帮我

4

0 回答 0