Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

swiftui - Disable the scrolling but not the content

I'm currently working on a swiftUI project where I'm supposed to be able to interact with the content of a scrollView. The problem is that I want to scroll only using buttons. It works but I can't figure a way to disable the scrolling without disabling the content of my ScrollView

Here's my code. I know that disabled(true) won't work but it's what I got so far

            ScrollViewReader { scrolling in
            ZStack{
                HStack{
                    ScrollView(.horizontal, showsIndicators: false){
                        HStack{
                            FirstStepView().id(0).padding(.horizontal,20).frame(width: UIScreen.main.bounds.width)
                            SecondStepView().id(1).frame(width: UIScreen.main.bounds.width)
                            ThirdStepView().id(2).frame(width: UIScreen.main.bounds.width)
                            FourthStepView().id(3).frame(width: UIScreen.main.bounds.width)
                        }
                    }.disabled(true)
                    
                    .animation(
                        Animation.easeOut(duration: 1)
                    )
                }
                .onChange(of: myId, perform: { value in
                    withAnimation{scrolling.scrollTo(myId)}
                })
            }
        }
question from:https://stackoverflow.com/questions/65844048/disable-the-scrolling-but-not-the-content

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't forget to provide a minimum reproducible example something we can copy and paste to ensure that the integrity if what you want is kept.

If your *StepView.count is static the code below might work for you

struct ButtonScroll: View {
    @State var myId: Int = 0
    
    init() {
        //Add this
        //It does NOT work with ScrollView
        UIScrollView.appearance().isScrollEnabled = false
    }
    var body: some View {
        VStack{
            //Simulates myId changing
            Button("change-position", action: {
                myId = Int.random(in: 0...3)
                print(myId.description)
            })
            ZStack{
                //TabView does not allow for changes in Tab/Page count nicely
                TabView(selection: $myId){
                    //Used Button to test interaction
                    Button("FirstStepView", action: {
                        print("FirstStepView")
                    }).tag(0)
                    Button("SecondStepView", action: {
                        print("SecondStepView")
                    }).tag(1)
                    Button("ThirdStepView", action: {
                        print("ThirdStepView")
                    }).tag(2)
                    Button("FourthStepView", action: {
                        print("FourthStepView")
                    }).tag(3)
                }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                .animation(
                    Animation.easeOut(duration: 1)
                )
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...