1 Star 0 Fork 0

raychow-github/ScrollViewLoader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ScrollViewLoader

A simple utility to assist with loading more content in a ScrollView in SwiftUI.

Usage

Add .shouldLoadMore to any ScrollView. By default it will be triggered when the content at the bottom of the ScrollView is close to being in view.

See it in action

If you want to see it in a real app, check out dateit

Also works well with SwiftUI-Refresher

Navigation

Usage

First add the package to your project.

import ScrollViewLoader

struct ContentView: View {
    
    @State var data: [Int] = Array(0..<1)
    
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(data, id: \.self) { i in
                    Text("\(i)")
                        .font(.title)
                        .frame(maxWidth: .infinity)
                }
                ProgressView()
                    .scaleEffect(2)
            }
        }
        .shouldLoadMore {
            await Task.sleep(seconds: 0.05)
            data.append(data.last! + 1)
        }
    }
}

Customization

By default, the callback will be triggered when distance to the bottom of the scrollable content is less than 50% of the visible hight of the scroll view. You can customize this

Set the relative offset to 20% instead of the default 50%:

.shouldLoadMore(bottomDistance: .relative(0.2)) { 
    // Load more
}

Set the absolute offset to a fixed value:

.shouldLoadMore(bottomDistance: .absolute(200)) { 
    // Load more
}

waitForHeightChange

It may be desirable for shouldLoadMore to be called whenever the user scrolls - even if the scroll view content didn't change. You can change this behavior with waitForHeightChange:

.shouldLoadMore(waitForHeightChange: .never) { 
    // Will be called regardless of if the content height changed from a previous update
}
.shouldLoadMore(waitForHeightChange: .always) { 
    // Will only be called if the content height changed since last time
}
.shouldLoadMore(waitForHeightChange: .until(2)) { 
    // Will only be called if the content height changed since last time or after 2 seconds of no change
}

By default waitForHeightChange is .until(2) so the function doesn't get called in quick succession when no content updates are made.

More details

  • The callback will only be called once when the bottom approaches.
  • If you scroll back up out of the trigger zone, it will be called again when you scroll back down.
  • It is up to you to synchronize and de-duplicate multiple scroll triggers by the user (depending on the kind of data you are loading)
  • Loading conditions will be re-evaluated if the scroll view content changes in any way.

More Examples

Using a completion handler instead of async

.shouldLoadMore { done in
    loadYourContent {
        data.append(data.last! + 1)
        done() // Call done so shouldLoadMore can be called again later
    }
}

Larger batching

Navigation

MIT License Copyright (c) 2022 Brian Floersch Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (1)

全部

语言

近期动态

11个月前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/raychow-github/ScrollViewLoader.git
git@gitee.com:raychow-github/ScrollViewLoader.git
raychow-github
ScrollViewLoader
ScrollViewLoader
master

搜索帮助