iOS_黑(7)_MJRefresh设置最短刷新时间

一丶

有时候网络太快,一下拉,数据马上出来,效果不好看;
需要弄个最短刷新时间;

思路:
1.记录开始刷新时间

  [self.mj_header beginRefreshingWithCompletionBlock:^{
         @strongify(self);
        self.ZB_beginRefreshTime = [NSDate date];
    }];

2.结束刷新时间

//判断时间间隔,处理时间
[self.mj_header endRefreshing]

二丶代码

.h

#import 

@interface UIScrollView (Refresh)


@property (nonatomic, strong) NSDate *ZB_beginRefreshTime;


/**
 头部刷新
 */
- (void)HYD_headerRefreshingBlock:(CompleteBlock)block;
- (void)HYD_headerRefreshingWithTarget:(id)target refreshingAction:(SEL)action;

/**
 开始刷新
 */
- (void)HYD_beginRefreshing;

/**
 尾部刷新
 */
- (void)HYD_footerRefreshingBlock:(CompleteBlock)block;
- (void)HYD_footerRefreshingWithTarget:(id)target refreshingAction:(SEL)action;

/**
 结束刷新
 */
- (void)HYD_endRefreshing;

@end

.m

//
//  UIScrollView+Refresh.m
//  hyb
//
//  Created by xzb on 16/10/11.
//  Copyright © 2016年 hyb. All rights reserved.
//

#import "UIScrollView+Refresh.h"

#import 
#import 


static const void *kProperty_ZB_beginRefreshTime = &kProperty_ZB_beginRefreshTime;

@implementation UIScrollView (Refresh)


- (void)setZB_beginRefreshTime:(NSDate *)ZB_beginRefreshTime{
    objc_setAssociatedObject(self, kProperty_ZB_beginRefreshTime, ZB_beginRefreshTime, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSDate *)ZB_beginRefreshTime{
    return objc_getAssociatedObject(self, kProperty_ZB_beginRefreshTime);
}


#pragma mark - 生命周期

#pragma mark - api
- (void)HYD_headerRefreshingWithTarget:(id)target refreshingAction:(SEL)action {
    [self
     configurationRefreshWithHedaer:[MJRefreshNormalHeader
                                     headerWithRefreshingTarget:target
                                     refreshingAction:action]];
}
- (void)HYD_headerRefreshingBlock:(CompleteBlock)block {
    [self configurationRefreshWithHedaer:[MJRefreshNormalHeader
                                          headerWithRefreshingBlock:^{
                                              
                                              BLOCK_EXEC(block);
                                          }]];
}
- (void)HYD_beginRefreshing {
    @weakify(self);
    [self.mj_header beginRefreshingWithCompletionBlock:^{
         @strongify(self);
        self.ZB_beginRefreshTime = [NSDate date];
    }];
}
- (void)HYD_footerRefreshingWithTarget:(id)target refreshingAction:(SEL)action {
    self.mj_footer =
    [MJRefreshBackNormalFooter footerWithRefreshingTarget:target
                                         refreshingAction:action];
}
- (void)HYD_footerRefreshingBlock:(CompleteBlock)block {
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        BLOCK_EXEC(block);
    }];
}
- (void)HYD_endRefreshing {
    
- (void)HYD_endRefreshing {
    
    
    if ([self.mj_header isRefreshing]) {
        if (!self.ZB_beginRefreshTime) {
            [self.mj_header endRefreshing];
        }else{
            @autoreleasepool {
                NSDate* toDate = [NSDate date];
                NSDate*  startDate    = self.ZB_beginRefreshTime;
                NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian ];
                NSUInteger unitFlags = NSCalendarUnitSecond ;
                NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate  toDate:toDate  options:0];
                NSInteger diffSec = [cps second];
                if (diffSec < 1) {
                    @weakify(self);
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        @strongify(self);
                        if([self.mj_header isRefreshing])[self.mj_header endRefreshing];
                    });
                }else{
                    [self.mj_header endRefreshing];
                }
            }
        }
        
    }
    if ([self.mj_footer isRefreshing]) {
        [self.mj_footer endRefreshing];
    }

}

#pragma mark - getter / setter

#pragma mark - model event

#pragma mark - view event

#pragma mark - private
- (void)configurationRefreshWithHedaer:(MJRefreshNormalHeader *)header {
    //隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    self.mj_header = header;
}
#pragma mark - delegete

#pragma mark - notification

#pragma mark - kvo

@end

三丶总结:

优化app体验;不爽就要改;

你可能感兴趣的:(iOS_黑(7)_MJRefresh设置最短刷新时间)