在 SwiftUI 2.0 中禁用选项卡视图滑动以更改页面

Tai*_*rif 4 tabview swipe-gesture swift swiftui swiftui-tabview

我在 SwiftUI 应用程序中使用选项卡视图。我想禁用其向左滑动并写入以移至其他页面。我检查了这个答案,也检查了这个,但没有一个有效。他们正在使用

.gesture(DragGesture())

这将禁用向左滑动。我想禁用向左和向右滑动。

我也尝试过:

.禁用(真)

这会禁用整个视图。我的视图上有一些按钮可以执行一些任务。这也让他们失去了能力。

我的代码是:

@State var selectedIndex = 0

var body: some View {

 TabView(selection: $selectedIndex) {
                
                FirstView().tag(0).gesture(DragGesture())
                
                SecondView().tag(1).gesture(DragGesture())
                
                ThirdView().tag(2).gesture(DragGesture())
                
            }.tabViewStyle(.page(indexDisplayMode: .never))
    }
Run Code Online (Sandbox Code Playgroud)

我的FirstView代码是:

func firstView() -> some View {
        
        VStack(alignment: .leading) {
     
            HStack {
                
                Text("Is this a live claim?")
                    .font(.custom(InterFont.bold.rawValue, size: 24))
                
                Spacer()
                
            }.padding()
            
            
            Spacer()
            
            BlueButton(title: "Continue") {
                
               print("pressed")
            }.padding()
            
        }.ignoresSafeArea(edges: .top)
    }
Run Code Online (Sandbox Code Playgroud)

我的SecondView代码是:

func secondView() -> some View {
        
        VStack(alignment: .leading) {
            
            HStack {
                Text("Detail of the incident")
                    .font(.custom(InterFont.bold.rawValue, size: 24))
                
                Text("(Optional)")
                    .font(.custom(InterFont.regular.rawValue, size: 14))
                
            }.padding()
            
            VStack(alignment: .leading) {
                
                Text("Detail of the incident")
                    .font(.custom(InterFont.regular.rawValue, size: 16))
                
                
                Spacer()
                
                BlueButton(title: "Continue", action: {
                    print("continue pressed")
                })
                
            }.padding()
            
        }.ignoresSafeArea(edges: .top)
    }
Run Code Online (Sandbox Code Playgroud)

我的ThirdView代码是:

func thirdView() -> some View {
        
            
                VStack(alignment: .leading, spacing: 20) {
                
                    VStack(spacing: -10) {
                        HStack{
                            
                            Text("Call Police Crime Number")
                                .font(.custom(InterFont.bold.rawValue, size: 14))
                            
                            Spacer()
                            
                            Text("101")
                                .font(.custom(InterFont.bold.rawValue, size: 14))
                                .underline()
                        }.padding()
                            
                    }
                    
                    Spacer()
                    
                    BlueButton(title: "Continue") {
                        
                            print("continue pressed")
                        
                    }.padding()
                 
                }.ignoresSafeArea(edges: .top)
                
            }
Run Code Online (Sandbox Code Playgroud)

模拟器

有人有解决方案吗?

Asp*_*eri 7

实际上,问题是因为您的视图是透明的,因此由于命中测试失败而忽略了手势。

修复方法是使用修饰符显式使其可进行命中测试.contentShape

使用 Xcode 13.4 / iOS 15.5 进行测试

TabView(selection: $selectedIndex) {
                
    FirstView().tag(0)
      .contentShape(Rectangle()).gesture(DragGesture())  // << here !!
    
    SecondView().tag(1)
      .contentShape(Rectangle()).gesture(DragGesture())
    
    ThirdView().tag(2)
      .contentShape(Rectangle()).gesture(DragGesture())

}.tabViewStyle(.page(indexDisplayMode: .never))
Run Code Online (Sandbox Code Playgroud)

  • 好吧,你实际上可以做到。如果底部有按钮,则可以使用 .simultaneousGesture(DragGesture()) 而不是 .gesture(DragGesture()) 与 .contentShape(Rectangle()) 结合使用 (2认同)