实现下拉刷新

#import <UIKit/UIKit.h>

#import "FreshTableHeardView.h"


@interface TableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource,FreshTableHeardViewDelegate>

{

    

    FreshTableHeardView *_refreshHeaderView;

    BOOL _reloading;

    

}

- (void)reloadTableViewDataSource;

- (void)doneLoadingTableViewData;


@end

#import "TableViewController.h"


@interface TableViewController ()


@end


@implementation TableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //设置导航控制器不透明

    self.navigationController.navigationBar.translucent = NO;

    //创建下拉时显示的view

    if (_refreshHeaderView == nil) {

    _refreshHeaderView = [[FreshTableHeardView alloc] initWithFrame:CGRectMake(0, 0 - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];

    _refreshHeaderView.delegate = self;

    [self.tableView addSubview:_refreshHeaderView];

    }

    [_refreshHeaderView refreshLastUpdatedDate];

    

}



#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    

    return 10;

}


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


    return 4;

}



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

    

    static NSString *identifer = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];

    

    if (cell == nil) {

        

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];

        

    }

    return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    

    return [NSString stringWithFormat:@"Section %ld", section];

    

}


#pragma mark -

#pragma mark Data Source Loading / Reloading Methods


- (void)reloadTableViewDataSource{

    

        _reloading = YES;

    

}


- (void)doneLoadingTableViewData{

    

   

    _reloading = NO;

    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];

    

}



#pragma mark -

#pragma mark UIScrollViewDelegate Methods


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    

    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];

    

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    

    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];

    

}



#pragma mark -

#pragma mark EGORefreshTableHeaderDelegate Methods


- (void)egoRefreshTableHeaderDidTriggerRefresh:(FreshTableHeardView*)view{

    

    [self reloadTableViewDataSource];

    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3];

    

}


- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(FreshTableHeardView*)view{

    

    return _reloading;

    

}


- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(FreshTableHeardView*)view{

    

    return [NSDate date]; 

    

}



#pragma mark -

#pragma mark MemoryManagement


- (void)didReceiveMemoryWarning {

    

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

    _refreshHeaderView=nil;

}



@end


#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>


//定义类型

typedef enum{


    EGOOPullRefreshPulling = 0,//下拉

    EGOOPullRefreshNormal,//正常

    EGOOPullRefreshLoading,//下拉之后

    

} PullRefreshState;


@protocol FreshTableHeardViewDelegate;

@interface FreshTableHeardView : UIView{



   

    PullRefreshState _state;

    

    UILabel *_lastUpdatedLabel;

    UILabel *_statusLabel;

    CALayer *_arrowImage;

    UIActivityIndicatorView *_activityView;


}

//下拉之后显示的数据

@property (nonatomic ,assign) id<FreshTableHeardViewDelegate> delegate;


- (void)refreshLastUpdatedDate;

- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView;

- (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView;

- (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView;



@end


@protocol FreshTableHeardViewDelegate

- (void)egoRefreshTableHeaderDidTriggerRefresh:(FreshTableHeardView *)view;

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(FreshTableHeardView *)view;

@optional

//日期

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(FreshTableHeardView *)view;

@end

#import "FreshTableHeardView.h"


@interface FreshTableHeardView (Private)

- (void)setState:(PullRefreshState)aState;

@end;


@implementation FreshTableHeardView


- (id)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        

        

        //下拉时显示的label

        _lastUpdatedLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 30, self.frame.size.width, 20)];

        _lastUpdatedLabel.font = [UIFont systemFontOfSize:12];

        _lastUpdatedLabel.shadowColor = [UIColor colorWithWhite:0 alpha:1];

        _lastUpdatedLabel.shadowOffset = CGSizeMake(0, 1);

        _lastUpdatedLabel.backgroundColor = [UIColor clearColor];

        _lastUpdatedLabel.textAlignment = NSTextAlignmentCenter;

        [self addSubview:_lastUpdatedLabel];

        

        

        //还原时显示的label

        _statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 48, self.frame.size.width, 20)];

        _statusLabel.font = [UIFont boldSystemFontOfSize:13];

        _statusLabel.shadowColor = [UIColor colorWithWhite:0.9 alpha:1];

        _statusLabel.shadowOffset = CGSizeMake(0, 1);

        _statusLabel.backgroundColor = [UIColor clearColor];

        _statusLabel.textAlignment = NSTextAlignmentCenter;

        [self addSubview:_statusLabel];

        

        //添加的图片

        _arrowImage = [CALayer layer];

        _arrowImage.frame = CGRectMake(25, frame.size.height - 65, 30, 55);

        _arrowImage.contentsGravity = kCAGravityResizeAspect;

        _arrowImage.contents = (id)[UIImage imageNamed:@"blueArrow.png"].CGImage;

        [[self layer] addSublayer:_arrowImage];

        

        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {

            _arrowImage.contentsScale = [[UIScreen mainScreen] scale];

        }

        

        //风火轮

        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        _activityView.frame = CGRectMake(25, frame.size.height - 38, 20, 20);

        [self addSubview:_activityView];

       

        [self setState:EGOOPullRefreshNormal];


      

        }

    return self;

    

}

//显示下拉刷新时的事件

- (void)refreshLastUpdatedDate{


   

    NSDate *date = [_delegate egoRefreshTableHeaderDataSourceLastUpdated:self];

    

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setAMSymbol:@"AM"];

    [formatter setPMSymbol:@"PM"];

    [formatter setDateFormat:@"MM/dd/yyyy hh:mm:a"];

    _lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:date]];


}

//下拉刷新的label

- (void)setState:(PullRefreshState)aState

{

    switch (aState) {

        case EGOOPullRefreshPulling:

            

            _statusLabel.text = @"Release to refresh...";

            [CATransaction begin];

            [CATransaction setAnimationDuration:0.18];

            _arrowImage.transform = CATransform3DMakeRotation((M_PI / 180) * 180, 0, 0, 1);

            [CATransaction commit];

            

            break;

        case EGOOPullRefreshNormal:

            

            if (_state == EGOOPullRefreshPulling) {

                [CATransaction begin];

                [CATransaction setAnimationDuration:0.18];

                _arrowImage.transform = CATransform3DIdentity;

                [CATransaction commit];

            }

            

            _statusLabel.text = @"Pull down to refresh...";

            [_activityView stopAnimating];

            [CATransaction begin];

            _arrowImage.hidden = NO;

            _arrowImage.transform = CATransform3DIdentity;

            [CATransaction commit];

            

            [self refreshLastUpdatedDate];

            break;

        case EGOOPullRefreshLoading:

            

            _statusLabel.text = @"Loading...";

            [_activityView startAnimating];

            [CATransaction begin];

            _arrowImage.hidden = YES;

            [CATransaction commit];

            

            break;

        default:

            break;

    }

    

    _state = aState;


}

//view没有滚动效果、所以定义一个UIScrollView

- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView

{

    if (_state == EGOOPullRefreshLoading) {

        

        CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);

        offset = MIN(offset, 60);

        scrollView.contentInset = UIEdgeInsetsMake(offset, 0, 0, 0);

        

    }else if (scrollView.isDragging) {

        


        if (_state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65 && scrollView.contentOffset.y < 0) {

            [self setState:EGOOPullRefreshNormal];

        } else if (_state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65) {

            [self setState:EGOOPullRefreshPulling];

        }

        

        if (scrollView.contentInset.top != 0) {

            scrollView.contentInset = UIEdgeInsetsZero;

        }

        

    }


   



}

- (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView

{


    if (scrollView.contentOffset.y <= -65) {

        

        [_delegate egoRefreshTableHeaderDidTriggerRefresh:self];

        

        [self setState:EGOOPullRefreshLoading];

        [UIView beginAnimations:nil context:NULL];

        [UIView setAnimationDuration:0.2];

        scrollView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);

        [UIView commitAnimations];

        

    }



}

- (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView

{


    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3];

    [scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];

    [UIView commitAnimations];

    

    [self setState:EGOOPullRefreshNormal];



}




@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值