
In this post, we’ll learn how to use a drag gesture to check the scrolling direction of a list. The goal is to hide the tab bar and change the appearance of a floating button based on whether the list is scrolling up or down, similar to the behavior of the Gmail app. (Check the code from previous post floating-button-in-swiftui).
First step: Define the two different styles for the Floating Button:
struct FullFB: View {
var body: some View {
Label("write", systemImage: "pencil")
.font(.title.weight(.semibold))
.padding()
.background(.pink)
.foregroundStyle(.white)
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 40, height: 40)))
.shadow(radius: 4, x: 0, y: 4)
}
}
struct SmallFB: View {
var body: some View {
Image(systemName: "pencil")
.font(.title.weight(.semibold))
.padding()
.background(.pink)
.foregroundStyle(.white)
.clipShape(Circle())
.shadow(radius: 4, x: 0, y: 4)
}
}
The code should be self-explanatory: the first style is a rounded rectangle button featuring both text and an icon, while the second style is a circular button with only an icon.
Now take a look to the list:
List(initList(), id:\.self) { mail in
HStack {…