0

I have the following code:

struct TestItem:Identifiable {
    var id = UUID()
    var index:Int
}


struct ContentView: View {
    
    @State var testItems = [TestItem]()
    let itemsUpperBound:Int = 1000
    
    @State var trigger = false
 
    var columnGridItems = [
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    
    var body: some View {
        
        VStack {
            HStack {
                Button {
                    trigger.toggle()
                    print("trigger: \(trigger)")
                } label: {
                    Text("TEST")
                        .foregroundColor(.white)
                        .padding()
                        .background(.blue)
                }
                Spacer()
            }
            
            ScrollView(.horizontal) {
                ScrollViewReader { sr in
                    LazyHGrid(rows: columnGridItems) {
                        ForEach(testItems) { ti in
                            Text("id \(ti.id)")
                        }
                    }
                    .border(.blue)
                    .onChange(of: trigger) { newValue in
                        withAnimation(.easeInOut(duration: 10)) {
                            let idx = Int.random(in: 0..<testItems.count)
                            let id = testItems[idx].id
                            print("will try to scroll to id: \(id)")
                            sr.scrollTo(id)
                        }
                    }
                }
            }
            .border(.red)
            
        }
        .onAppear {
            for i in 0..<itemsUpperBound {
                testItems.append(TestItem(index: i))
            }
        }
        
    }
    
}

I would like to animate the scroll view content offset, with whatever duration I'd like to set, so that the app would show the entire content of the scrollview without requiring the user to scroll it. In UIKit I could do it by animating the content offset, but, I am not sure how to do this in SwiftUI.

How can I animate the content offset of a scrollview in SwiftUI to achieve this type of animation?

I have edited the code to account for Asperi's comment and tried to use a ScrollReader. It does scroll to the desired item in the scrollview, but the duration is not working. I set it to 10 seconds, and it doesn't seem to do anything.

How can I animate the scroll using a custom duration I want?

enter image description here

6
  • Does this answer your question stackoverflow.com/a/62587784/12299030? Commented Jun 28, 2022 at 11:10
  • I can't seem to make the scroll animation take the duration I set. It just scrolls using the standard animation time. Commented Jun 28, 2022 at 16:56
  • Yes, type of animation is not configured, it is known behavior - it just animates (similarly as List btw) Commented Jun 28, 2022 at 17:01
  • Is there any other way to animate with custom animation duration in SwiftUI? Commented Jun 28, 2022 at 17:04
  • 1
    No, implement custom scroll view (demo stackoverflow.com/a/58708206/12299030) then you'll have full control under everything. Commented Jun 28, 2022 at 17:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.