SwiftUI 中使用BBSwiftUIKit开源库实现上拉加载和下拉刷新
import SwiftUI
import BBSwiftUIKit
struct ContentView: View {
@State var list : [Int] = (0..<50).map{$0}
@State var isRefreshing: Bool = false
@State var isLoadingMore: Bool = false
@State var isReloadData: Bool = false
var body: some View {
BBTableView(list) { i in
Text("Text\(i)")
.padding()
.background(Color.blue)
}
//调用该方法后isRefreshing会变为true
.bb_pullDownToRefresh(isRefreshing: $isRefreshing) {
print("Refresh")
//设置为一秒后停止刷新
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.list = (0..<50).map{$0}
self.isRefreshing = false
}
}
.bb_pullUpToLoadMore(bottomSpace: 40) {
if self.isLoadingMore{return}
self.isLoadingMore = true
print("Loading More")
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
let more = (self.list.count..<self.list.count + 10)
self.list.append(contentsOf: more)
self.isLoadingMore = false
}
}
.bb_reloadData($isReloadData)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
BBScrollView的使用
import SwiftUI
import BBSwiftUIKit
struct ScrollViewTest: View {
@State var contentOffset: CGPoint = .zero
@State var contentOffsetToScrollAnimated: CGPoint? = CGPoint(x: 200.0, y: 0.0)
var body: some View {
let list : [Int] = (0..<50).map{$0}
BBScrollView(.horizontal, contentOffset: $contentOffset) {
// Add views
HStack(spacing:0) {
ForEach(list,id:\.self) { i in
Text("\(i)")
.frame(width: UIScreen.main.bounds.width/10, height: 30, alignment: .center)
.background(Color.blue)
}
}
}
.bb_bounces(false) // 反弹效果
.bb_isPagingEnabled(true) // 分页效果
.bb_showsHorizontalScrollIndicator(false) // 是否显示水平滚动条
.bb_showsVerticalScrollIndicator(false)
}
}
struct ScrollViewTest_Previews: PreviewProvider {
static var previews: some View {
ScrollViewTest()
}
}