V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
whoami9426
V2EX  ›  Swift

请教 SwiftUI 布局的问题

  •  
  •   whoami9426 · 42 天前 · 1423 次点击
    这是一个创建于 42 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想要实现一个上下分页滑动,左右分页滑动的交互效果

     var body: some View{
      ScrollView(showsIndicators: false){
                LazyVStack(spacing:0){
                    ForEach(1...10, id: \.self){ index in
                        ScrollView(.horizontal,showsIndicators: false){
                            LazyHStack(spacing:0){
                                
                                VStack {
                                    Text("Left View \(index)")
                                        .font(.system(.largeTitle))
                                        .foregroundStyle(.white)
                                }
                                .frame(maxWidth: .infinity, maxHeight: .infinity)
                                .background(Color.black)
                                .containerRelativeFrame([.horizontal,.vertical])
                               
                                
                                VStack {
                                    Text("Right View \(index)")
                                        .font(.system(.largeTitle))
                                        .foregroundStyle(.white)
                                }
                                .frame(maxWidth: .infinity, maxHeight: .infinity)
                                .background(Color.blue)
                                .containerRelativeFrame([.horizontal,.vertical])
                                
                            }.scrollTargetLayout()
                        }
                        .ignoresSafeArea()
                        .containerRelativeFrame([.horizontal,.vertical])
                        .scrollTargetBehavior(.paging)
                        .onAppear(perform: {
                            UIScrollView.appearance().bounces = false
                            UIScrollView.appearance().alwaysBounceVertical = false
                            UIScrollView.appearance().alwaysBounceHorizontal = false
                        })
                    }
                }.scrollTargetLayout()
            }
            .ignoresSafeArea()
            .containerRelativeFrame([.horizontal,.vertical])
            .scrollTargetBehavior(.paging)
    }
    

    目前用这种方式实现,但是有个问题,滑动到 RightView 可以上下滑动到下一层,有什么办法在滑动到 RightView 时只允许左滑回 Left View

    2 条回复    2024-07-27 22:53:34 +08:00
    kemchenj
        2
    kemchenj  
       42 天前
    最外层的 ScrollView 可以用 scrollDisabled(:_) 去控制是否禁用滚动手势,在内层的 ScrollView 用 GeometryReader 之类的监控一下 offset ,判断是否需要禁用滚动手势
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2251 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 02:02 · PVG 10:02 · LAX 19:02 · JFK 22:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.