当前位置:网站首页>Swiftui tutorial how to realize automatic scrolling function in 2 seconds

Swiftui tutorial how to realize automatic scrolling function in 2 seconds

2022-07-07 11:45:00 Knowledge fatness

image.png

Use SwiftUI It's amazing to develop . It does make it possible to implement some cool functions in just a few lines of code . today , Let's realize automatic scrolling . When you create a chat application , This technique is particularly useful . I will explain it as briefly as possible .

Prepare your UI

struct AutoScrollList: View {
    var body: some View {
        
        VStack {
            Button("Scroll to 100") {
                
            }
            List(1...500, id: \.self) { index in
                Text("\(index)")
            }
        }
    
    }
}

image.png

I implemented a very simple list with a button . When the user clicks the button , I want my list ( Scroll view ) Scroll to a position . How do I achieve it ?

Wrap the entire view ScrollViewReader

struct AutoScrollList: View {
    var body: some View {
        ScrollViewReader { proxy in
            VStack {
                Button("
原网站

版权声明
本文为[Knowledge fatness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/07/202207070945546026.html