简体   繁体   中英

change view location smoothly in SwiftUI

I am trying to change a specific view location in my app as smoothly as possible. The condition on the change is when a text is change via a textfield:

struct ExploreView: View {
    @State var searchText : String = ""
    var body: some View {
        ZStack{
            Color(searchText == "" ? "RedColor" : "BlueColor").edgesIgnoringSafeArea(.top)
            VStack {
                if searchText == "" {
                    Spacer()
                }
                searchBarBottom(searchText: $searchText).shadow(radius: 5).padding(.vertical)
                if searchText != "" {
                    Spacer()
                }
            }
        }
    }
}

here is searchBarBottom:

struct searchBarBottom: View {
    @Binding var searchText : String
    var body: some View {
        HStack{
            HStack{
                if searchText == "" {
                    Image(systemName: "magnifyingglass").foregroundColor(.black)
                }
                TextField("search", text: $searchText)
                if searchText != "" {
                    Button(action: {
                        self.searchText = ""
                    }, label: {
                        Text("cancel")
                    })
                }
            }.padding(.horizontal).frame(height: 36).background(Color("GrayColor")).cornerRadius(5)
            if searchText == "" {
                HStack{
                    NavigationLink(destination: AddSpotView()) {
                        Image(systemName: "plus").resizable().frame(width: 17, height: 17).foregroundColor(.black)
                    }.frame(width: 36, height: 36).background(Color("GrayColor")).cornerRadius(5)
                    NavigationLink(destination: ExploreList()) {
                        Image(systemName: "list.bullet").resizable().frame(width: 17, height: 15).foregroundColor(.black)
                        }.frame(width: 36, height: 36).background(Color("GrayColor")).cornerRadius(5)
                }
            }
            }.padding(.horizontal).frame(width: UIScreen.main.bounds.width/1.1, height: 55).background(Color.white).clipped().shadow(radius: 5).cornerRadius(10)
    }
}

the change is as follows:

state when searchText = "" 在此处输入图像描述

state when searchText != "" 在此处输入图像描述

I am currently using Spacer() because that is the easiest way I found but the change is really rough and I want it to be smooth and easy on the eye. Any ideas on how I might achieve that?

Thanks!

Just add animation (tested with Xcode 11.4 / iOS 13.4)

    ZStack{
        Color(searchText == "" ? "RedColor" : "BlueColor").edgesIgnoringSafeArea(.top)
        VStack {
            if searchText == "" {
                Spacer()
            }
            searchBarBottom(searchText: $searchText).shadow(radius: 5).padding(.vertical)
            if searchText != "" {
                Spacer()
            }
        }.animation(.default)       // << here !!
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM