BBSwiftUIKit
一个具有强大 UIKit 特性的 SwiftUI 库。
特性
- 获取和设置滚动视图的
contentOffset
- 设置滚动视图的
isPagingEnabled
,bounces
以及其他属性 - 下拉或上拉表格视图以刷新数据或加载更多数据
- 重新加载表格视图数据或行
- 将表格视图滚动到特定行
- 获取和设置页控制器的
currentPage
要求
- iOS 13.0+
- Swift 5
安装
使用 CocoaPods 安装
- 将
pod 'BBSwiftUIKit'
添加到您的 Podfile 中。 - 运行
pod install
或pod update
。 - 将
import BBSwiftUIKit
添加到 Swift 源文件中。
如何使用
滚动视图
使用具有UIScrollView
功能的BBScrollView
。
// Content offset binding with scroll view content offset
@State var contentOffset: CGPoint = .zero
// Set CGPoint value to scroll to the content offset with animation. After scrolling, it will reset to nil automatically
@State var contentOffsetToScrollAnimated: CGPoint? = nil
// Create a scroll view scrolling horizontally
// Get and set content offset
BBScrollView(.horizontal, contentOffset: $contentOffset) {
// Add views
HStack {
...
}
}
.bb_bounces(false) // Disable bounces
.bb_isPagingEnabled(true) // Enable paging
.bb_alwaysBounceHorizontal(true) // Enable always bounce horizontal
.bb_showsHorizontalScrollIndicator(false) // Hide horizontal scroll indicator
.bb_contentOffsetToScrollAnimated($contentOffsetToScrollAnimated) // Set content offset with animation
表格视图
使用具有UITableView
功能的BBTableView
。
class Model: ObservableObject {
// Table view data source
@Published var list: [Int] = ...
// Set true to reload data. After reloading, it will reset to false automatically
@Published var reloadData: Bool = false
// Set row indexes to reload rows. After reloading, it will reset to empty automatically
@Published var reloadRows: [Int] = []
// Set row index to scroll to the specific row. After scrolling, it will reset to nil automatically
@Published var scrollToRow: Int? = nil
// Pull down to refresh data. While refreshing, this value is true. After refreshing, we need to set it false to end refreshing
@Published var isRefreshing: Bool = false
// Pull up to loading more data. We should manage this value to prevent calling load more function too many times
@Published var isLoadingMore: Bool = false
}
// Create a table view with data source
BBTableView(model.list) { item in
// Add views
}
.bb_reloadData($model.reloadData) // Reload data
.bb_reloadRows($model.reloadRows, animation: .automatic) // Reload rows
.bb_scrollToRow($model.scrollToRow, position: .none, animated: true) // Scroll to row
.bb_setupRefreshControl { refreshControl in
// Update refresh control appearance
refreshControl.tintColor = .blue
refreshControl.attributedTitle = NSAttributedString(string: "Loading...", attributes: [.font: UIFont.systemFont(ofSize: 15), .foregroundColor: UIColor.blue])
}
.bb_pullDownToRefresh(isRefreshing: $model.isRefreshing) {
// Pull down to refresh data
// Now the value of isRefreshing is true
self.loadNewData { newData in
self.model.list = newData // Update data source
self.model.isRefreshing = false // End refreshing
}
}
.bb_pullUpToLoadMore(bottomSpace: 30) {
// Pull up to load more data
if self.model.isLoadingMore { return } // Do not load more if it is loading more
self.model.isLoadingMore = true // Mark it is loading more
self.loadMoreData { moreData in
self.model.list.append(moreData) // Update data source
self.model.isLoadingMore = false // Mark it is NOT loading more
}
}
许可
BBSwiftUIKit遵循MIT许可证发布。有关详细信息,请参阅LICENSE。