pod 'RefreshControl'
#import "UIScrollView+RefreshControl.h"
顶部RefreshControl
__weak typeof(self) weakSelf = self;
[self.tableView addTopRefreshControlUsingBlock:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// request for datas
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
[weakSelf.tableView topRefreshControlStopRefreshing];
});
} refreshControlPullType:RefreshControlPullTypeInsensitive refreshControlStatusType:RefreshControlStatusTypeText];
注意:在回调块中,您应该首先重新加载数据,然后停止 TopRefreshControl
动画,否则,您的内容焦点将移至顶部。
底部RefreshControl
__weak typeof(self) weakSelf = self;
[self.tableView addBottomRefreshControlUsingBlock:^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// request for datas
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.tableView reloadData];
[weakSelf.tableView bottomRefreshControlStopRefreshing];
});
} refreshControlPullType:RefreshControlPullTypeSensitive refreshControlStatusType:RefreshControlStatusTypeText];
注意:在回调块中,您应该始终首先重新加载数据,然后停止 BottomRefreshControl
动画,否则,您的内容底部将始终移至屏幕底部。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.tableView topRefreshControlStartInitializeRefreshing];
});
typedef NS_ENUM(NSInteger, RefreshControlPullType) {
RefreshControlPullTypeSensitive,
RefreshControlPullTypeInsensitive
};
typedef NS_ENUM(NSInteger, RefreshControlStatusType) {
RefreshControlStatusTypeTextAndArrow,
RefreshControlStatusTypeText,
RefreshControlStatusTypeArrow
};
通过刷新控制 pullsType 设置 RefreshControlPullTypeInsensitive
顶部RefreshControl
addTopRefreshControlUsingBlock:refreshControlPullType:refreshControlStatusType
底部RefreshControl
addBottomRefreshControlUsingBlock:refreshControlPullType:refreshControlStatusType
您可以根据需要更改状态文本和颜色,加载动画的圆圈颜色和箭头颜色。
@property (nonatomic, strong) NSString *topRefreshControlPullToRefreshingText;
@property (nonatomic, strong) NSString *topRefreshControlPullReleaseToRefreshingText;
@property (nonatomic, strong) NSString *bottomRefreshControlPullToRefreshingText;
@property (nonatomic, strong) NSString *bottomRefreshControlPullReleaseToRefreshingText;
@property (nonatomic, strong) UIColor *statusTextColor;
@property (nonatomic, strong) UIColor *loadingCircleColor;
@property (nonatomic, strong) UIColor *arrowColor;
您可以为刷新失败设置状态文本
topRefreshControlRefreshFailureWithHintText:
bottomRefreshControlRefreshFailureWithHintText:
您可以自己处理触摸事件
addTouchUpInsideEventForTopRefreshControlUsingBlock:
addTouchUpInsideEventForBottomRefreshControlUsingBlock:
如果您不处理此事件,则在您触摸 RefreshControl 或再次拉动时,我们会重新刷新。当然,在您处理此事件后,如果还想刷新,请调用流程消息或再次拉动。
topRefreshControlResumeRefreshing
bottomRefreshControlResumeRefreshing