美文网首页
MJRefresh头部和尾部的用法

MJRefresh头部和尾部的用法

作者: Silence_xl | 来源:发表于2019-04-04 14:07 被阅读0次

尾部

//
//  HNRefreshFooter.m
//  headlineNews
//
//  Created by dengweihao on 2017/11/20.
//  Copyright © 2017年 vcyber. All rights reserved.
//

#import "HNRefreshFooter.h"
#import "UIView+AnimationExtend.h"
@interface HNRefreshFooter()
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic)UIImageView *imageViewLoading;

@end

@implementation HNRefreshFooter
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置(比如添加子控件)
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont boldSystemFontOfSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_add_video_16x16_"]];
    logo.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logo];
    self.imageViewLoading = logo;
    self.automaticallyHidden = YES;
}
#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    self.label.frame = self.bounds;
    self.imageViewLoading.bounds = CGRectMake(0, 0, 16, 16);
    self.imageViewLoading.center = CGPointMake(self.mj_w * 0.5 + 60, self.mj_h * 0.5);
    
}
#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            self.label.text = @"上拉加载数据";
            self.imageViewLoading.hidden = NO;
            [self.imageViewLoading stopRotationAnimation];
            break;
        case MJRefreshStateRefreshing:
            self.label.text = @"正在努力加载";
            self.imageViewLoading.hidden = NO;
            [self.imageViewLoading rotationAnimation];
            break;
        case MJRefreshStateNoMoreData:
            self.label.text = @"木有数据了";
            self.imageViewLoading.hidden = YES;
            [self.imageViewLoading stopRotationAnimation];
            break;
        default:
            break;
    }
}
@end
//
//  UIView+AnimationExtend.m
//  headlineNews
//
//  Created by dengweihao on 2017/11/20.
//  Copyright © 2017年 vcyber. All rights reserved.
//

#import "UIView+AnimationExtend.h"

@implementation UIView (AnimationExtend)
- (CABasicAnimation *)rotationAnimation {
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = @0;
    rotationAnimation.toValue = @(M_PI * 2.0);
    rotationAnimation.duration = 2;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    return rotationAnimation;
}
- (void)stopRotationAnimation {
    
    if ([self.layer animationForKey:@"rotationAnimation"]) {
        [self.layer removeAnimationForKey:@"rotationAnimation"];
    }
}

@end

头部

//
//  HNRefreshGifHeader.m
//  headlineNews
//
//  Created by dengweihao on 2017/11/20.
//  Copyright © 2017年 vcyber. All rights reserved.
//

#import "HNRefreshGifHeader.h"

#define HNRefreshStateRefreshingImagesCount 16

@interface HNRefreshGifHeader()

@property (weak, nonatomic) UILabel *label;

@end
@implementation HNRefreshGifHeader

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置(比如添加子控件)
- (void)prepare
{
    [super prepare];
    // 设置普通状态的动画图片
    self.mj_h = 50;
    
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i< HNRefreshStateRefreshingImagesCount; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%lu", (unsigned long)i]];
        [refreshingImages addObject:image];
    }
    [self setImages:refreshingImages forState:MJRefreshStateIdle];
    [self setImages:refreshingImages forState:MJRefreshStatePulling];
    [self setImages:refreshingImages forState:MJRefreshStateRefreshing];
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont boldSystemFontOfSize:10];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.lastUpdatedTimeLabel.hidden = YES;
    self.stateLabel.hidden = YES;
    self.label = label;
}
#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    self.label.frame = CGRectMake(0, self.mj_h - 15, self.mj_w, 15);
    self.gifView.frame = CGRectMake((self.mj_w - 25) / 2.0, 5, 25, 25);
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            self.label.text = @"下拉推荐";
            break;
        case MJRefreshStatePulling:
            self.label.text = @"松开推荐";
            break;
        case MJRefreshStateRefreshing:
            self.label.text = @"推荐中";
            break;
        default:
            break;
    }
}
@end

MJRefresh的实现原理
利用分类在UITableView或UICollectionView上添加MJRefreshHeaderView,MJRefreshFooterView,监听方法监听滑动的距离区改变状态,在setState方法中做相应的事情.

相关文章

网友评论

      本文标题:MJRefresh头部和尾部的用法

      本文链接:https://www.haomeiwen.com/subject/qbifiqtx.html