简体   繁体   English

SwiftUI 使用 TabView 和 Picker

[英]SwiftUI use TabView with Picker

I'm trying to use a Picker to change the selected tab.我正在尝试使用Picker来更改选定的选项卡。

The issue is that when I tap on the picker, the content changes without animation.问题是当我点击选择器时,内容会发生变化而没有 animation。

Scrolling between tabs works correctly (the picker animates accordingly).在选项卡之间滚动正常工作(选择器相应地动画)。

在此处输入图像描述

struct ContentView: View {
@State var tabSelectedValue = 0

var body: some View {

    VStack {
        
        Picker("", selection: $tabSelectedValue) {
            Text("First").tag(0)
            Text("Second").tag(1)
        }
        .pickerStyle(SegmentedPickerStyle())
        .padding()
         
        TabView(selection: $tabSelectedValue) {
         
            Text("Content for first tab")
                .tag(0)
        
            Text("Content for second tab")
                .tag(1)
            
            
        }
        .tabViewStyle(.page(indexDisplayMode: .never))
    }
    .padding()
}

} }

Picker with SegmentedPickerStyle have default animation. SegmentedPickerStylePicker具有默认的 animation。 So when you change the tab it's animating Picker .因此,当您更改选项卡时,它会为Picker设置动画。 To animate TabView when you change tabSelectedValue you need to add the animation ViewModier to your TabView .要在更改TabView时为tabSelectedValue设置动画,您需要将 animation ViewModier添加到TabView Add animation(_:value:) modifier to your TabView .animation(_:value:)修饰符添加到您的TabView

.animation(.easeIn, value: tabSelectedValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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