使用案例
// 调用hw_hearderRefreshBlock自动添加下拉刷新
_tableView.hw_hearderRefreshBlock = ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.tableView endRefreshing];
});
};
// 调用hw_footerRefreshBlock自动添加上拉刷新
_tableView.hw_footerRefreshBlock = ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.tableView endRefreshing];
});
};
.h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIScrollView (HWCategory)
/** 直接回调下拉刷新 无需添加 */
@property(nonatomic, copy) void(^hw_hearderRefreshBlock)(void);
/** 直接回调上拉刷新 无需添加 */
@property(nonatomic, copy) void(^hw_footerRefreshBlock)(void);
/** 结束上下拉刷新 */
- (void)endRefreshing;
/** 开始下拉刷新 */
- (void)beginRefreshing;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "UIScrollView+HWCategory.h"
#import <MJRefresh/MJRefresh.h>
#import <objc/runtime.h>
static NSString * const hearderRefreshBlockKey = @"hearderRefreshBlockKey";
static NSString * const footerRefreshBlockKey = @"footerRefreshBlockKey";
@implementation UIScrollView (HWCategory)
- (void)setHw_hearderRefreshBlock:(void (^)(void))hw_heardreRefreshBlock {
if (self.mj_header == nil) {
[self hw_addHearderRefresh];
}
objc_setAssociatedObject(self, (__bridge const void * _Nonnull)(hearderRefreshBlockKey), hw_heardreRefreshBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(void))hw_hearderRefreshBlock {
return objc_getAssociatedObject(self, (__bridge const void * _Nonnull)(hearderRefreshBlockKey));
}
- (void)setHw_footerRefreshBlock:(void (^)(void))hw_footerRefreshBlock {
if (self.mj_footer == nil) {
[self hw_addFooterRefresh];
}
objc_setAssociatedObject(self, (__bridge const void * _Nonnull)(footerRefreshBlockKey), hw_footerRefreshBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(void))hw_footerRefreshBlock {
return objc_getAssociatedObject(self, (__bridge const void * _Nonnull)(footerRefreshBlockKey));
}
- (void)hw_addHearderRefresh {
__weak typeof(self) weakSelf = self;
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf footerEndRefreshing];
if ([weakSelf.mj_header isRefreshing]) {
if (weakSelf.hw_hearderRefreshBlock != nil) {
weakSelf.hw_hearderRefreshBlock();
}
}
}];
self.mj_header = header;
}
- (void)hw_addFooterRefresh {
__weak typeof(self) weakSelf = self;
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
[weakSelf headerEndRefreshing];
if ([weakSelf.mj_footer isRefreshing]) {
if (weakSelf.hw_footerRefreshBlock != nil) {
weakSelf.hw_footerRefreshBlock();
}
}
}];
[self.mj_footer endRefreshing];
self.mj_footer = footer;
}
- (void)endRefreshing {
[self headerEndRefreshing];
[self footerEndRefreshing];
}
- (void)headerEndRefreshing {
if ([self.mj_header isRefreshing]) {
[self.mj_header endRefreshing];
}
}
- (void)footerEndRefreshing {
if ([self.mj_footer isRefreshing]) {
[self.mj_footer endRefreshing];
}
}
- (void)beginRefreshing {
[self.mj_header beginRefreshing];
}
@end
网友评论