MJRefresh的自定义动画

1.下拉动画

先继承于MJRefreshGifHeader


屏幕快照 2019-11-28 下午1.31.29.png

.m中代码

#import "CustomGifHeader.h"

@implementation CustomGifHeader

#pragma mark - 实现父类的方法
- (void)prepare {
    [super prepare];
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=60; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", I]];
        [idleImages addObject:image];
    }
    
    // 设置即将刷新状态的动画图片(一松开就会刷新的状态)
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd", I]];
        [refreshingImages addObject:image];
    }
    
    // 设置普通状态的动画图片
    [self setImages:idleImages forState:MJRefreshStateIdle];
    
    //即将刷新状态
    [self setImages:refreshingImages forState:MJRefreshStatePulling];
    
    // 设置正在刷新状态的动画图片
    [self setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
}
- (void)placeSubviews {
    [super placeSubviews];
    //隐藏状态显示文字
   self.stateLabel.hidden = YES;

    //隐藏更新时间文字
   self.lastUpdatedTimeLabel.hidden = YES;
}
    

2.上拉动画

先继承于MJRefreshBackGifFooter


屏幕快照 2019-11-28 下午1.35.22.png

.m中的代码

#import "CustomGifFooter.h"

@implementation CustomGifFooter

#pragma mark - 实现父类的方法
- (void)prepare {
    [super prepare];
    //GIF数据
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=8; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Loading_%zd.tiff",I]];
        [idleImages addObject:image];
    }
    
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=8; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Loading_%zd.tiff",I]];
        [refreshingImages addObject:image];
    }
    //普通状态
    [self setImages:idleImages forState:MJRefreshStateIdle];
    //即将刷新状态
    [self setImages:refreshingImages forState:MJRefreshStatePulling];
    //正在刷新状态
    [self setImages:refreshingImages forState:MJRefreshStateRefreshing];

}
- (void)placeSubviews {
    [super placeSubviews];
    //隐藏状态显示文字
   // self.stateLabel.hidden = YES;
    [self setTitle:@"加载中.." forState:MJRefreshStateIdle];
    [self setTitle:@"加载中.." forState:MJRefreshStatePulling];
    [self setTitle:@"加载中.." forState:MJRefreshStateRefreshing];
    [self setTitle:@"没有更多了" forState: MJRefreshStateNoMoreData];
}

3.使用方法

#import "ViewController.h"
#import "CustomGifHeader.h"
#import "CustomGifFooter.h"
#import "CustomAutoGifFooter.h"

@interface ViewController ()
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)CustomGifHeader * gifHeader;
@property(nonatomic,strong)CustomGifFooter * gifFooter;
@property(nonatomic,assign)NSInteger number;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"自定义刷新动画";
    self.number=5;
    [self.view addSubview:self.tableView];

}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return   self.number;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.textLabel.text = @"假装有内容";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    return cell;
}
#pragma mark---懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.mj_header = self.gifHeader;
        _tableView.mj_footer = self.gifFooter;
    }
    return _tableView;
}
- (CustomGifHeader *)gifHeader {
    if (!_gifHeader) {
        _gifHeader = [CustomGifHeader headerWithRefreshingBlock:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.number=5;
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            });
        }];
    }
    return _gifHeader;
}

- (CustomGifFooter *)gifFooter {
    if (!_gifFooter) {
        _gifFooter = [CustomGifFooter footerWithRefreshingBlock:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.number=self.number+1;
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            });
        }];
    }
    return _gifFooter;
}

自己的随心笔录,希望给您带来帮助。

注:只是自己的随心笔录,文中有引用别人内容若有侵权请及时联系我

你可能感兴趣的:(MJRefresh的自定义动画)