FigRefresh
FigRefresh是一个Swift刷新框架。它的API看起来像MJRefresh。FigRefresh简单且可扩展。
用法
import FigRefresh
scrollView.fig_header = RefreshIndicatorHeader(refreshingClosure: { [weak self] in
DispatchQueue.global().async {
sleep(3)
DispatchQueue.main.async {
self?.scrollView.fig_header?.endRefreshing()
}
}
})
在资源中查找FigRefreshDemo项目。
设计
RefreshComponent有一个titleLabel属性,它根据不同状态显示不同的文本。我建议你在这它的子类中自定义titleLabel。
我支持3种实现类型:
- 1、文本。
- 2、指示器 + 文本。
- 3、图片 + 文本。
如果这些不能满足您,只需继承RefreshHeader和RefreshFooter类。
public class MyCustomHeader:RefreshHeader{
//Custom UI
var myImage:UIImageView?
var myDetailsLabel:UILabel?
...
//Return height of UI. 50 is default value
public override func refreshComponentHeight() -> CGFloat {
return 50
}
//Just excute once. This method used to init UI.
public override func refreshComponentDidMoveToSuperview() {
//must call
super.refreshComponentDidMoveToSuperview()
//Custom styles of myImage
//Add myImage
addSubview(myImage)
//Layout myImage
...
//Custom styles of myDetailsLabel
//Add myDetailsLabel
addSubview(myDetailsLabel)
//Layout myDetailsLabel
...
//If you meed custom titleLabel
titleLabel?.textColor = UIColor.gray
titleLabel?.textAlignment = .left
titleLabel!.font = UIFont.systemFont(ofSize: 13)
//Option1: Layout titleLabel use constraints
// titleLabel?.snp.remakeConstraints({ (make) in
// make.left.equalTo(animationIV.snp.right).offset(10)
// make.centerY.equalToSuperview()
// make.height.equalToSuperview()
// make.right.equalToSuperview()
// })
//Option2: Layout titleLabel use frame
titleLabel?.frame = CGRect(x: animationIV.frame.maxX + 10, y: 0, width: frame.width - (animationIV.frame.maxX + 10), height: height)
...
}
public override func refreshComponentStateChange(state: RefreshState) {
super.refreshComponentStateChange(state: state)
switch state{
case .refreshing: //custom style on refreshing state
...
break
default: //custom style on idle state
...
break
}
}
}
状态
FigRefresh有6种状态
- idle:正常空闲
- pullingInRect:拉动且组件未完全显示
- pullingOutRect:拉动并组件完全显示
- releaseing:释放,不刷新,从pullingInRect状态。
- refreshing:释放,并刷新,从pullingOutRect状态。
- noMoreData:没有更多数据(只有底部)
RefreshHeader和RefreshFooter类,支持状态上的默认文本:
open class RefreshHeader:RefreshHeaderControl{
...
open override func refreshComponentTitlesWithStates() -> [RefreshState : String] {
return [.idle:"Pull down to refresh",
.pullingInRect:"Pull down to refresh",
.pullingOutRect:"Release to refresh",
.releaseing:"Pull down to refresh",
.refreshing:"Loading..."]
}
}
open class RefreshFooter:RefreshFooterControl{
...
open override func refreshComponentTitlesWithStates() -> [RefreshState : String] {
return [.idle: "Pull up to load more",
.pullingInRect: "Pull up to load more",
.pullingOutRect: "Release to load more",
.releaseing:"Pull up to load more",
.refreshing:"Loading...",
.noMoreData:"No more data"]
}
}
最佳实践
1、继承 RefreshHeader 和 RefreshFooter。
- 1、添加新属性。
- 1、重写 refreshComponentTitlesWithStates 方法以自定义文本。
- 2、重写 refreshComponentDidMoveToSuperview 方法以自定义 UI。
2、设置全局头部和尾部。
设置一个头部和尾部。
FigRefreshSetDefaultHeader(MyCustomRefreshHeader.self)
FigRefreshSetDefaultFooter(MyCustomRefreshFooter.self)
使用 fig_header 调用头部。使用 fig_footer 调用尾部。
scrollView.fig_header {
DispatchQueue.global().async { [weak self] in
sleep(3)
DispatchQueue.main.async {
self?.scrollView.fig_header?.endRefreshing()
}
}
}
scrollView.fig_footer { [weak self] in
DispatchQueue.global().async {
sleep(3)
DispatchQueue.main.async {
self?.scrollView.fig_footer?.endRefreshing()
}
}
}
3、动态修改头部和尾部。
tableView.fig_footer?.setTitle("xxxx", for: .noMoreData) // If needed.
tableView.fig_footer?.endRefreshingWithNoMoreData()
4、使用与全局不同的自定义头部或尾部。
scrollView.fig_header = MyCustom2Header(refreshingClosure: { [weak self] in
DispatchQueue.global().async {
sleep(3)
DispatchQueue.main.async {
self?.scrollView.fig_header?.endRefreshing()
}
}
})
需求
- iOS 8.0+
- Xcode 10.2
- Swift 5
安装
Cocoapods
在您的 Podfile 中添加以下行
pod "FigRefresh"
作者
周 Kevin
- 邮件: [email protected]
- Twitter: @wumingapie
- Facebook: wumingapie
- LinkedIn: Rafael
许可证
FigRefresh 在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。