这是从一个分支中复制来的 https://github.com/samvermette/SVPullToRefresh。它修改了 SVInfiniteScrolling,将其状态变为 SVInfiniteScrollingStateTriggered,当拖动以显示所有 SVInfiniteScrolling 时。
这些 UIScrollView 分类使得将拖动刷新和无限滚动功能添加到任何 UIScrollView(或其任何子类)变得超级简单。SVPullToRefresh 使用 Objective-C 运行时向 UIScrollView
添加以下 3 个方法,而不是依赖于代理和/或将 UIViewController
作为子类。
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler;
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler position:(SVPullToRefreshPosition)position;
- (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
重要提示:如果你的项目不使用 ARC:你必须在目标设置 > 编译签名 > 编译源中添加 -fobjc-arc
编译器标志到 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
}];
或者如果您想要从底部进行拖动刷新
[tableView addPullToRefreshWithActionHandler:^{
// prepend data to dataSource, insert cells at top of table view
// call [tableView.pullToRefreshView stopAnimating] when done
} position:SVPullToRefreshPositionBottom];
如果您想要在代码中引发刷新(例如,在 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
。
它使用键值观察来跟踪 scrollView 的 contentOffset
。
SVPullToRefresh 由 Sam Vermette 和 项目的贡献者 提供。如果您有功能建议或错误报告,请通过发送拉取请求或通过 创建新问题 来帮助解决。如果您正在您的项目中使用 SVPullToRefresh,则适当的致谢将会很棒。
特别感谢 @seb_morel 为其 揭开 Objective-C 运行时面纱 演讲,这对本项目的帮助很大。
向 Loren Brichter 致敬,他为发明下拉刷新做出了贡献。