实现下拉刷新

来源:互联网 发布:淘宝账号怎么找回 编辑:程序博客网 时间:2024/05/20 04:51

#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 {

    [superviewDidLoad];

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

    self.navigationController.navigationBar.translucent = NO;

    //创建下拉时显示的view

    if (_refreshHeaderView ==nil) {

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

    _refreshHeaderView.delegate =self;

    [self.tableViewaddSubview:_refreshHeaderView];

    }

    [_refreshHeaderViewrefreshLastUpdatedDate];

    

}



#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 = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifer];

        

    }

   return cell;

}

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

    

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

    

}


#pragma mark -

#pragma mark Data Source Loading / Reloading Methods


- (void)reloadTableViewDataSource{

    

       _reloading = YES;

    

}


- (void)doneLoadingTableViewData{

    

   

    _reloading = NO;

    [_refreshHeaderViewegoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];

    

}



#pragma mark -

#pragma mark UIScrollViewDelegate Methods


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

    

    [_refreshHeaderViewegoRefreshScrollViewDidScroll:scrollView];

    

}


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

    

    [_refreshHeaderViewegoRefreshScrollViewDidEndDragging:scrollView];

    

}



#pragma mark -

#pragma mark EGORefreshTableHeaderDelegate Methods


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

    

    [selfreloadTableViewDataSource];

    [selfperformSelector:@selector(doneLoadingTableViewData)withObject:nilafterDelay:3];

    

}


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

    

    return_reloading;

    

}


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

    

   return [NSDatedate]; 

    

}



#pragma mark -

#pragma mark MemoryManagement


- (void)didReceiveMemoryWarning {

    

    [superdidReceiveMemoryWarning];

}

- (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 = [superinitWithFrame:frame]) {

        

        

        //下拉时显示的label

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

       _lastUpdatedLabel.font = [UIFontsystemFontOfSize:12];

       _lastUpdatedLabel.shadowColor = [UIColorcolorWithWhite:0alpha:1];

       _lastUpdatedLabel.shadowOffset =CGSizeMake(0,1);

        _lastUpdatedLabel.backgroundColor = [UIColorclearColor];

        _lastUpdatedLabel.textAlignment =NSTextAlignmentCenter;

        [selfaddSubview:_lastUpdatedLabel];

        

        

        //还原时显示的label

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

        _statusLabel.font = [UIFontboldSystemFontOfSize:13];

       _statusLabel.shadowColor = [UIColorcolorWithWhite:0.9alpha:1];

       _statusLabel.shadowOffset =CGSizeMake(0,1);

        _statusLabel.backgroundColor = [UIColorclearColor];

        _statusLabel.textAlignment =NSTextAlignmentCenter;

        [selfaddSubview:_statusLabel];

        

       //添加的图片

       _arrowImage = [CALayerlayer];

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

        _arrowImage.contentsGravity =kCAGravityResizeAspect;

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

        [[selflayer] addSublayer:_arrowImage];

        

        if ([[UIScreenmainScreen] respondsToSelector:@selector(scale)]) {

           _arrowImage.contentsScale = [[UIScreenmainScreen] 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);

        

    }elseif (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
原创粉丝点击