下拉刷新是一种利用手势刷新用户界面的功能,虽然已经被Twitter申请为专利,但依然不能阻止广大的App开发者在自己的应用中加入该功能。苹果公司甚至在iOS6的sdk中加入了UIRefreshControl,从而实现了系统级的下拉刷新。但是UIRefreshControl是绑定在UITableViewController上的,所以灵活性不高。
如果在网上搜下拉刷新的实现,讨论最多的恐怕是EGOTableViewPullRefresh了,这是一个比较有历史的开源库了,github上最近一次提交都是两年前的事情了。EGOTableViewPullRefresh虽然很好的实现了下拉刷新功能,但是一些OC的新特性比如arc、block没有得到支持,导致加个下拉刷新要写不少代码。
于是乎EGOTableViewPullRefresh的替代者出来了,就是SVPullToRefresh。支持arc,支持block,而且简洁到只需一行就能实现下拉刷新和上拉加载。
SVPullToRefresh的下拉刷新用法相当简单:
1、将下拉刷新控件放在顶部
[tableView addPullToRefreshWithActionHandler:^{
// prepend data to dataSource, insert cells at top of table view
// call [tableView.pullToRefreshView stopAnimating] when done
}];
2、将下拉刷新控件放在底部
[tableView addPullToRefreshWithActionHandler:^{
// prepend data to dataSource, insert cells at top of table view
// call [tableView.pullToRefreshView stopAnimating] when done
} position:SVPullToRefreshPositionBottom];
3、程序自动调用下拉刷新
[tableView triggerPullToRefresh];
4、临时性禁用下拉刷新
tableView.showsPullToRefresh = NO;
SVPullToRefresh的UI支持自定义
下拉刷新对应的view名叫pullToRefreshView,有如下属性和方法修改它的显示。
@property (nonatomic, strong) UIColor *arrowColor;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
- (void)setTitle:(NSString *)title forState:(SVPullToRefreshState)state;
- (void)setSubtitle:(NSString *)subtitle f