测试已测试 | ✗ |
语言语言 | Objective-CObjective C |
许可证 | MIT |
发布最后发布 | 2017年11月 |
由 Scott Corscadden 维护。
这些 UIScrollView 分类使得向任何 UIScrollView(或其子类)添加下拉刷新和无限滚动功能变得非常简单。SVPullToRefresh 不依赖于代理和/或 UIViewController
的子类化,而是使用 Objective-C 运行时在 UIScrollView
中添加以下 3 个方法:
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler;
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler position:(SVPullToRefreshPosition)position;
- (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
将 pod 'SVPullToRefresh'
添加到您的 Podfile 中或如果您喜欢冒险,请添加 pod 'SVPullToRefresh', :head
注意:如果您的项目未使用 ARC,则必须将编译器标志 -fobjc-arc
添加到 Target Settings > Build Phases > Compile Sources 中的 UIScrollView+SVPullToRefresh.m
和 UIScrollView+SVInfiniteScrolling.m
SVPullToRefresh/SVPullToRefresh
文件夹拖到您的项目中。UIScrollView+SVPullToRefresh.h
和/或 UIScrollView+SVInfiniteScrolling.h
(请参阅 /Demo
中的示例 Xcode 项目)
[tableView addPullToRefreshWithActionHandler:^{
// prepend data to dataSource, insert cells at top of table view
// call [tableView.pullToRefreshView stopAnimating] when done
}];
如果要程序化触发刷新(例如在 viewDidAppear:
中),可以使用以下方法
[tableView triggerPullToRefresh];
您可以通过设置 showsPullToRefresh
属性来暂时隐藏下拉刷新视图
tableView.showsPullToRefresh = NO;
以下属性和方法可以用于自定义下拉刷新视图
@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 forState:(SVPullToRefreshState)state;
- (void)setCustomView:(UIView *)view forState:(SVPullToRefreshState)state;
您可以通过滚动视图的 pullToRefreshView
属性访问这些属性。
例如,您可以使用以下方法设置 arrowColor
属性
tableView.pullToRefreshView.arrowColor = [UIColor whiteColor];
[tableView addInfiniteScrollingWithActionHandler:^{
// append data to data source, insert new cells at the end of table view
// call [tableView.infiniteScrollingView stopAnimating] when done
}];
如果您要在 viewDidAppear:
程序化地触发加载(例如),可以使用以下方法
[tableView triggerInfiniteScrolling];
您可以通过设置 showsInfiniteScrolling
属性来暂时隐藏无限滚动视图
tableView.showsInfiniteScrolling = NO;
以下方法可以用于自定义无限滚动视图
- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle;
- (void)setCustomView:(UIView *)view forState:(SVInfiniteScrollingState)state;
您可以通过滚动视图的 infiniteScrollingView
属性访问这些属性。
SVPullToRefresh 通过添加新的公共方法和动态属性扩展了 UIScrollView
它通过键值观察跟踪滚动视图的 contentOffset
。
SVPullToRefresh 是由 Sam Vermette 和 项目贡献者 共同带来的。如果您有任何功能建议或错误报告,请通过发送拉取请求或在 创建新问题 的方式提供帮助。如果您的项目中使用了 SVPullToRefresh,给予归属是很好的。
非常感谢 @seb_morel 的 Demistifying the Objective-C runtime 演讲,它对这一项目非常有帮助。
感谢 Loren Brichter 发明下拉刷新功能。