该文章阅读的AFNetworking的版本为3.2.0。
这个分类可以为UIRefreshControl
绑定一个task
,然后根据task
的加载状态自动开始和结束刷新。
1.接口文件
/** 为UIRefreshControl绑定指定的task */- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;复制代码
2.AFRefreshControlNotificationObserver私有类
2.1.属性
/** 保存要绑定task的刷新控件 */@property (readonly, nonatomic, weak) UIRefreshControl *refreshControl;复制代码
2.2.方法
/** 以指定刷新控件初始化的方法 */- (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl;/** 为UIRefreshControl绑定指定的task */- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;复制代码
2.3.实现
- 接口方法的实现
- (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl{ self = [super init]; if (self) { // 用属性保存传入的参数 _refreshControl = refreshControl; } return self;}- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task { // 创建通知中心对象 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; // 先移除掉之前添加通知 [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil]; [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil]; [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil]; // 如果传入的task不为nil if (task) { // 获取到传入的刷新控件 UIRefreshControl *refreshControl = self.refreshControl; // 如果task的状态是正在运行中 if (task.state == NSURLSessionTaskStateRunning) { // 刷新控件开始刷新 [refreshControl beginRefreshing]; // 注册通知监听task的开始、完成和暂停 [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingTaskDidResumeNotification object:task]; [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidCompleteNotification object:task]; [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidSuspendNotification object:task]; // 如果task的状态不是是正在运行中 } else { // 刷新控件停止刷新 [refreshControl endRefreshing]; } }}复制代码
- 通知回调方法的实现
- (void)af_beginRefreshing { // 在主队列异步调用刷新控件的开始刷新 dispatch_async(dispatch_get_main_queue(), ^{ [self.refreshControl beginRefreshing]; });}- (void)af_endRefreshing { // 在主队列异步调用刷新控件的停止刷新 dispatch_async(dispatch_get_main_queue(), ^{ [self.refreshControl endRefreshing]; });}复制代码
- 生命周期方法实现
- (void)dealloc { // 移除掉添加通知 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil]; [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil]; [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];}复制代码
3.方法实现
- (AFRefreshControlNotificationObserver *)af_notificationObserver { // 先通过关联对象获取为分类创建的属性 AFRefreshControlNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver)); // 如果属性为空 if (notificationObserver == nil) { // 创建刷新控件通知观察者对象 notificationObserver = [[AFRefreshControlNotificationObserver alloc] initWithActivityRefreshControl:self]; // 通过关联对象将创建的对象保存到属性中 objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } return notificationObserver;}- (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task { // 获取到刷新控件通知观察者对象并为之绑定task [[self af_notificationObserver] setRefreshingWithStateOfTask:task];}复制代码
4.总结
可以看到这个分类的实现基本和UIActivityIndicatorView+AFNetworking
分类的实现一模一样。都是依靠分类内部的私有类通过对绑定task
相关属性添加通知观察,来控制刷新控件的动作。
源码阅读系列:AFNetworking