【问题标题】:How to animate the page change with TabView on SwiftUIHow to animate the page change with TabView on SwiftUI
【发布时间】:2022-12-27 13:16:31
【问题描述】:

I'm trying to create a TabView and it works fine, but I would like to animate the view change, but all I could find is to animate with the PageTabViewStyle, but it makes the screen draggable, which is something I don't want to. I think I could remove this gesture, but the tabView gets tiny, as the iOS Home Screen indicators.

So, basically I would like to change the animation between the Views, here's my current code:

struct MainView: View {
@State private var selectedTab = 0
private var pageTitles = ["Home", "Home2"]

var body: some View {
    TabView(selection: $selectedTab) {
        HomeView(test: .green)
            .tabItem {
                Image(systemName: "house.fill")
                Text("Home")
            }.tag(0)
        HomeView(test: .blue)
            .tabItem {
                Image(systemName: "house.fill")
                Text("Home2")
            }.tag(1)
    }
    .navigationTitle(Text(pageTitles[selectedTab]))
    .navigationBarTitleDisplayMode(.inline)
}

}

【问题讨论】:

  • You can use a picker segmented control to select the visible view. So you can choose how you transition between view without swipe gestures.

标签: ios swiftui tabview


【解决方案1】:

I was having this problem myself. There's actually a pretty simple solution. Typically we supply a selection parameter just by using the binding shorthand as $selectedTab. However if we create a Binding explicitly, then we'll have the chance to apply withAnimation closure when updating the value:

@State private var selectedTab = Tabs.firstTab

TabView(
  selection: Binding<ModeSwitch.Value>(
    get: {
      selectedTab
    },
    set: { targetTab in
      withAnimation {
        selectedTab = targetTab
      }
    }
  ),
  content: {
    ...
  }
)

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2023-02-01
    • 2022-12-02
    • 2022-12-02
    • 2022-12-02
    • 2022-12-19
    • 2022-12-27
    • 2022-12-01
    • 2022-12-01
    相关资源
    最近更新 更多