Hello all,
Could someone please help me figure out a wait to animate switching between the two views that are items in the TabView below? Ideally, I’d like the transition to be something natural, e.g. swiping left while in CurrentContentView
moves it to the left and drags PreviousContentView
from the right, and vice versa when swithing back.
import SwiftUI
struct ContentView: View {
@State private var selectedTab = 0
let numTabs = 2
let minDrag: CGFloat = 50
// MARK: - BODY
var body: some View {
let currentDay = Calendar.current.component(.day, from: Date())
TabView(selection: $selectedTab) {
Group{
CurrentContentView()
.tabItem {
VStack{
Image(systemName: "\(currentDay).circle.fill")
Text("Current content")
}
}
.tag(0)
.highPriorityGesture(
DragGesture()
.onEnded({
self.handleSwipe(translation: $0.translation.width)
})
)
PreviousContentView()
.tabItem {
VStack{
Image(systemName: "clock.arrow.circlepath")
Text("Previous content")
}
}
.tag(1)
.highPriorityGesture(
DragGesture()
.onEnded({
self.handleSwipe(translation: $0.translation.width)
})
)
}
.toolbar(.visible, for: .tabBar)
.toolbarBackground(Color.white, for: .tabBar)
}
}
private func handleSwipe(translation: CGFloat) {
if translation > minDrag && selectedTab > 0 {
selectedTab -= 1
edge = .leading
}
else if translation < -minDrag && selectedTab < numTabs - 1 {
selectedTab += 1
edge = .trailing
}
}
}