使用 MJ 自定义下拉刷新

//
//  ViewController.m
//  Refresh
//
//  Created by Apple on 16/7/19.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "MJRefresh.h"
#import "DIYRefreshHeader.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    

    
    _dataArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];

    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
    self.tableView.mj_header = [DIYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.textLabel.text = _dataArr[indexPath.row];
    return cell;
}

#pragma mark - 数据处理相关
#pragma mark 下拉刷新数据
- (void)loadNewData {

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
//
//  DIYBackFooter.m
//  Refresh
//
//  Created by Apple on 16/7/20.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "DIYBackFooter.h"

@interface DIYBackFooter ()

@property (weak, nonatomic) UILabel *lable;
@property (weak, nonatomic) UIImageView *logoImg;
@property (weak, nonatomic) UIImageView *loadingImg;

@end
//在DIYBackFooter 这个类中,自定义
@implementation DIYBackFooter

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置(比如添加子控件)

- (void)prepare {
    
    [super prepare];
    
    //设置控件的高度
    self.mj_h = 60.0f;
    
    //添加 lable
    
    UILabel *lable = [[UILabel alloc] init];
    lable.font = [UIFont boldSystemFontOfSize:10.0f];
    lable.textAlignment = NSTextAlignmentCenter;
    [self addSubview:lable];
    self.lable = lable;
    
    //logo
    UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_logo.png"]];
    [self addSubview:logo];
    self.logoImg = logo;
    //loadingImg
    UIImageView *loading = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_logo_round"]];
    [self addSubview:loading];
    self.loadingImg = loading;
    
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews {
    
    [super placeSubviews];
    
    
    self.logoImg.frame = CGRectMake(0, 0, 26.0f, 26.0f);
    self.logoImg.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5 - 10.0f);
    
    self.loadingImg.frame = CGRectMake(0, 0, 36.0f, 36.0f);
    self.loadingImg.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5 - 10.0f);
    
    self.lable.frame = CGRectMake(self.mj_w * 0.5 - 40.0f, self.mj_h - 15.0f, 80.0f, 12.0f);
    
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle: {
            [self.loadingImg stopAnimating];
            self.lable.text = @"下拉刷新";
        }
            break;
        case MJRefreshStatePulling: {
            
            [self.loadingImg stopAnimating];
            self.lable.text = @"松开刷新";
            [self isAnimation];
        }
            break;
        case MJRefreshStateRefreshing: {
            
            self.lable.text = @"正在刷新";
            [self isAnimation];
            [self.loadingImg startAnimating];
        }
            break;
        default:
            break;
    }
}

- (void)isAnimation {
    
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI];
    rotationAnimation.duration = 3 * 0.075;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 10;
    
    [self.loadingImg.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
}

#pragma mark 监听拖拽比例(控件被拖出来的比例)
- (void)setPullingPercent:(CGFloat)pullingPercent {
    
    [super setPullingPercent:pullingPercent];
}

@end

 

posted @ 2016-08-02 08:53  _水畔竹汐  阅读(852)  评论(0)    收藏  举报