ios仿EGORefreshTableHeaderView上提下拉翻页插件

在资源中上传了该插件,先介绍其使用方法:

1、在需要使用的viewcontroller.h中,先

#import "EGORefreshTableHeaderView.h"

#import "EGORefreshTableTailView.h"

@interface RefreshTableViewController : UIViewController<EGORefreshTableHeaderDelegate,EGORefreshTableTailDelegate>

{

    IBOutlet UITableView *refreshTableView;

    IBOutletUINavigationBar *navBar;

    

    EGORefreshTableHeaderView *refreshHeaderView;

    EGORefreshTableTailView *refreshTailView;

    

    //  Reloading var should really be your tableviews datasource

    //  Putting it here for demo purposes

    BOOL reloading;

    BOOL tailReloading;

}

@property (nonatomic,strong)id<RefreshDelegate> refreshDelegate;

@property (nonatomic,retain)NSArray *tableContentArray;


- (void)doneLoadingTableViewData;

- (void)doneTailLoadingTableViewData;


在所需的viewcontroller.m文件中:

@implementation RefreshTableViewController


@synthesize refreshDelegate;

@synthesize tableContentArray;


- (void)viewDidLoad

{

    [superviewDidLoad];


    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

//    [[NSNotificationCenter defaultCenter] addObserver:self

//                                             selector:@selector(changeFrames:)

//                                                 name:UIDeviceOrientationDidChangeNotification

//                                               object:nil];

    

    [RefreshUtil setExtraCellLineHidden:refreshTableView]; //去除tableview底部多余线条

    

    [selfrefreshHeadView];

    [selfrefreshTailView];

}



- (void) refreshHeadView

{

    if (refreshHeaderView ==nil) {

LYEGORefreshTableHeaderView *view = [[LYEGORefreshTableHeaderViewalloc] initWithRefreshFrame:CGRectMake(0.0f,0.0f - refreshTableView.bounds.size.height,self.view.frame.size.width,refreshTableView.bounds.size.height)];

view.delegate =self;

[refreshTableViewaddSubview:view];

refreshHeaderView = view;

}

//  update the last update date

[refreshHeaderViewrefreshLastUpdatedDate];

}


- (void) refreshTailView

{

    CGFloat y = refreshTableView.contentSize.height;

    if (y <self.view.bounds.size.height) {

        y = self.view.bounds.size.height;

    }

    

    if (refreshTailView ==nil) {

EGORefreshTableTailView *view = [[EGORefreshTableTailViewalloc] initWithRefreshFrame:CGRectMake(0.0f, y,self.view.frame.size.width, y)];

view.delegate =self;

[refreshTableViewaddSubview:view];

refreshTailView = view;

}

   else {

        refreshTailView.frame =CGRectMake(refreshTailView.frame.origin.x, y, refreshTailView.frame.size.width,refreshTailView.frame.size.height);

    }

//  update the last update date

[refreshTailViewrefreshTailLastUpdatedDate];

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    // Return the number of sections.

   return 1;

}


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

{

    // Return the number of rows in the section.

    returntableContentArray.count;

}


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

{

   static NSString *CellIdentifier =@"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

    

    // Configure the cell...

    cell.textLabel.text =@"test";

    

   return cell;

}


/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

*/


/*

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }   

    else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

    }   

}

*/


/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

}

*/


/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    // Return NO if you do not want the item to be re-orderable.

    return YES;

}

*/


#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    // Navigation logic may go here. Create and push another view controller.

   /*

     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

     // ...

     // Pass the selected object to the new view controller.

     [self.navigationController pushViewController:detailViewController animated:YES];

     */

}


#pragma mark -

#pragma mark Data Source Loading / Reloading Methods


- (void)reloadTableViewDataSource

{

//  should be calling your tableviews data source model to reload

//  put here just for demo

reloading =YES;

    if (refreshDelegate !=nil) {

       if ([refreshDelegaterespondsToSelector:@selector(refreshWithFinish)]) {

            [refreshDelegaterefreshWithFinish];

           return;

        }

       else if ([refreshDelegaterespondsToSelector:@selector(refresh)]) {

            [refreshDelegaterefresh];

        }

    }

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

}


- (void)doneLoadingTableViewData

{

//  model should call this when its done loading

reloading =NO;

[refreshHeaderViewegoRefreshScrollViewDataSourceDidFinishedLoading:refreshTableView];

    

    if (refreshDelegate !=nil) {

       if ([refreshDelegaterespondsToSelector:@selector(reloadHeadView)]) {

            [refreshDelegatereloadHeadView];

        }

    }

}


#pragma mark -

#pragma mark Data Source Loading / Reloading Methods


- (void)reloadTableViewTailDataSource

{

//  should be calling your tableviews data source model to reload

//  put here just for demo

tailReloading =YES;

    

    if (refreshDelegate !=nil) {

       if ([refreshDelegaterespondsToSelector:@selector(refreshTailViewWithFinish)]) {

            [refreshDelegaterefreshTailViewWithFinish];

           return;

        }

       else if ([refreshDelegaterespondsToSelector:@selector(refreshTail)]) {

            [refreshDelegaterefreshTail];

        }

    }

    

    [selfperformSelector:@selector(doneTailLoadingTableViewData)withObject:nilafterDelay:1.0f];

}


- (void)doneTailLoadingTableViewData

{

//  model should call this when its done loading

tailReloading =NO;

[refreshTailViewegoRefreshScrollViewDataSourceDidFinishedLoading:refreshTableView];


    if (refreshDelegate !=nil) {

       if ([refreshDelegaterespondsToSelector:@selector(reloadView)]) {

            [refreshDelegatereloadView];

        }

    }

    

    [selfrefreshTailView];

    

    

}



#pragma mark -

#pragma mark UIScrollViewDelegate Methods


- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    if (!tailReloading) {

        [refreshHeaderViewegoRefreshScrollViewDidScroll:scrollView];

    }


   if (!reloading) {

       if (!tailReloading) {

            [selfrefreshTailView];

        }

        

        [refreshTailViewegoRefreshScrollViewDidScroll:scrollView];

    }

}


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

{

    if (!tailReloading) {

        [refreshHeaderViewegoRefreshScrollViewDidEndDragging:scrollView];

    }

   if (!reloading) {

        [refreshTailViewegoRefreshScrollViewDidTailEndDragging:scrollView];

    }

}



#pragma mark -

#pragma mark EGORefreshTableHeaderDelegate Methods


- (void)egoRefreshTableHeaderDidTriggerRefresh:(LYEGORefreshTableHeaderView*)view

{

[selfreloadTableViewDataSource];

}


- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(LYEGORefreshTableHeaderView*)view

{

returnreloading; // should return if data source model is reloading

}


- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(LYEGORefreshTableHeaderView*)view

{

return [NSDatedate]; // should return date data source was last changed

}


#pragma mark -

#pragma mark EGORefreshTableTailDelegate Methods


- (void)egoRefreshTableTailDidTriggerRefresh:(EGORefreshTableTailView*)view

{

[selfreloadTableViewTailDataSource];

}


- (BOOL)egoRefreshTableTailDataSourceIsLoading:(EGORefreshTableTailView*)view

{

returntailReloading;// should return if data source model is reloading

}


- (NSDate*)egoRefreshTableTailDataSourceLastUpdated:(EGORefreshTableTailView*)view

{

return [NSDatedate]; // should return date data source was last changed

}

以上就是使用方式,有些复杂,哈哈,不过懒得改了。
而插件在资源里。暂时未传的上去,就先把代码贴这里吧:
EGORefreshTableHeaderView.h文件如下:

//

//  LYEGORefreshTableHeaderView.h

//  Demo

//

//  Created by Devin Doty on 10/14/09October14.

//  Copyright 2009 enormego. All rights reserved.

//

//  Permission is hereby granted, free of charge, to any person obtaining a copy

//  of this software and associated documentation files (the "Software"), to deal

//  in the Software without restriction, including without limitation the rights

//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

//  copies of the Software, and to permit persons to whom the Software is

//  furnished to do so, subject to the following conditions:

//

//  The above copyright notice and this permission notice shall be included in

//  all copies or substantial portions of the Software.

//

//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

//  THE SOFTWARE.

//


#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>


#define TEXT_COLOR [UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0]

#define FLIP_ANIMATION_DURATION 0.18f


typedef enum{

EGOOPullRefreshPulling = 0,

EGOOPullRefreshNormal,

EGOOPullRefreshLoading,

} EGOPullRefreshState;


@protocol EGORefreshTableHeaderDelegate;

@interface LYEGORefreshTableHeaderView : UIView {

id _delegate;

EGOPullRefreshState _state;


UILabel *_lastUpdatedLabel;

UILabel *_statusLabel;

CALayer *_arrowImage;

UIActivityIndicatorView *_activityView;

}


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


- (id)initWithRefreshFrame:(CGRect)frame;

- (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor;


- (void)refreshLastUpdatedDate;

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

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

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


- (void)setState:(EGOPullRefreshState)aState;


@end


@protocol EGORefreshTableHeaderDelegate<NSObject>

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

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

@optional

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


@end


EGORefreshTableHeaderView.m文件如下:


//

//  LYEGORefreshTableHeaderView.m

//  Demo

//

//  Created by Devin Doty on 10/14/09October14.

//  Copyright 2009 enormego. All rights reserved.

//

//  Permission is hereby granted, free of charge, to any person obtaining a copy

//  of this software and associated documentation files (the "Software"), to deal

//  in the Software without restriction, including without limitation the rights

//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

//  copies of the Software, and to permit persons to whom the Software is

//  furnished to do so, subject to the following conditions:

//

//  The above copyright notice and this permission notice shall be included in

//  all copies or substantial portions of the Software.

//

//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

//  THE SOFTWARE.

//


#import "EGORefreshTableHeaderView.h"


//

//#define TEXT_COLOR [UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0]

//#define FLIP_ANIMATION_DURATION 0.18f



//@interface LYEGORefreshTableHeaderView (Private)

//- (void)setState:(EGOPullRefreshState)aState;

//@end


@implementation LYEGORefreshTableHeaderView


@synthesize delegate=_delegate;



- (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor  {

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

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;

self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];


UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

label.font = [UIFont systemFontOfSize:12.0f];

label.textColor = textColor;

label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];

label.shadowOffset = CGSizeMake(0.0f, 1.0f);

label.backgroundColor = [UIColor clearColor];

label.textAlignment = UITextAlignmentCenter;

[self addSubview:label];

_lastUpdatedLabel=label;

[label release];

label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 48.0f, self.frame.size.width, 20.0f)];

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

label.font = [UIFont boldSystemFontOfSize:13.0f];

label.textColor = textColor;

label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];

label.shadowOffset = CGSizeMake(0.0f, 1.0f);

label.backgroundColor = [UIColor clearColor];

label.textAlignment = UITextAlignmentCenter;

[self addSubview:label];

_statusLabel=label;

[label release];

CALayer *layer = [CALayer layer];

layer.frame = CGRectMake(25.0f, frame.size.height - 65.0f, 30.0f, 55.0f);

layer.contentsGravity = kCAGravityResizeAspect;

layer.contents = (id)[UIImage imageNamed:arrow].CGImage;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

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

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

}

#endif

[[self layer] addSublayer:layer];

_arrowImage=layer;

UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

view.frame = CGRectMake(25.0f, frame.size.height - 38.0f, 20.0f, 20.0f);

[self addSubview:view];

_activityView = view;

[view release];

[self setState:EGOOPullRefreshNormal];

    }

    return self;

}


- (id)initWithRefreshFrame:(CGRect)frame  {

  return [self initWithFrame:frame arrowImageName:@"blueArrow.png" textColor:TEXT_COLOR];

}


#pragma mark -

#pragma mark Setters


- (void)refreshLastUpdatedDate {

if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceLastUpdated:)]) {

NSDate *date = [_delegate egoRefreshTableHeaderDataSourceLastUpdated:self];

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

[dateFormatter setTimeStyle:NSDateFormatterShortStyle];


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

[[NSUserDefaults standardUserDefaults] setObject:_lastUpdatedLabel.text forKey:@"EGORefreshTableView_LastRefresh"];

[[NSUserDefaults standardUserDefaults] synchronize];

} else {

_lastUpdatedLabel.text = nil;

}


}


- (void)setState:(EGOPullRefreshState)aState{

switch (aState) {

case EGOOPullRefreshPulling:

_statusLabel.text = NSLocalizedString(@"Release to refresh...", @"Release to refresh status");

[CATransaction begin];

[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];

_arrowImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);

[CATransaction commit];

break;

case EGOOPullRefreshNormal:

if (_state == EGOOPullRefreshPulling) {

[CATransaction begin];

[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];

_arrowImage.transform = CATransform3DIdentity;

[CATransaction commit];

}

_statusLabel.text = NSLocalizedString(@"Pull down to refresh...", @"Pull down to refresh status");

[_activityView stopAnimating];

[CATransaction begin];

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; 

_arrowImage.hidden = NO;

_arrowImage.transform = CATransform3DIdentity;

[CATransaction commit];

[self refreshLastUpdatedDate];

break;

case EGOOPullRefreshLoading:

_statusLabel.text = NSLocalizedString(@"Loading...", @"Loading Status");

[_activityView startAnimating];

[CATransaction begin];

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; 

_arrowImage.hidden = YES;

[CATransaction commit];

break;

default:

break;

}

_state = aState;

}



#pragma mark -

#pragma mark ScrollView Methods


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

if (_state == EGOOPullRefreshLoading) {

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

offset = MIN(offset, 60);

scrollView.contentInset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);

} else if (scrollView.isDragging) {

BOOL _loading = NO;

if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceIsLoading:)]) {

_loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];

}

if (_state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_loading) {

[self setState:EGOOPullRefreshNormal];

} else if (_state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_loading) {

[self setState:EGOOPullRefreshPulling];

}

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

scrollView.contentInset = UIEdgeInsetsZero;

}

}

}


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

BOOL _loading = NO;

if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceIsLoading:)]) {

_loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];

}

if (scrollView.contentOffset.y <= - 65.0f && !_loading) {

[self setState:EGOOPullRefreshLoading];

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.2];

scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);

[UIView commitAnimations];

        if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDidTriggerRefresh:)]) {

[_delegate egoRefreshTableHeaderDidTriggerRefresh:self];

}

}

}


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

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.3];

[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];

[UIView commitAnimations];

[self setState:EGOOPullRefreshNormal];


}



#pragma mark -

#pragma mark Dealloc


- (void)dealloc {

    

_delegate=nil;

_activityView = nil;

_statusLabel = nil;

_arrowImage = nil;

_lastUpdatedLabel = nil;

    [super dealloc];

}



@end



EGORefreshTableTailView.h文件如下:

//

//  EGORefreshTableTailView.h

//  testPullAndPush

//

//  Created by liuyun on 9/4/13.

//  Copyright (c) 2013 liuyun. All rights reserved.

//


#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>

//#import "LYEGORefreshTableHeaderView.h"


typedef enum {

EGOOTailPullRefreshPulling = 0,

EGOOTailPullRefreshNormal,

EGOOTailPullRefreshLoading,

} EGOTailPullRefreshState;


@protocol EGORefreshTableTailDelegate;

@interface EGORefreshTableTailView : UIView

{

    __unsafe_unretained id delegate;

EGOTailPullRefreshState _state;

    

UILabel *_lastUpdatedLabel;

UILabel *_statusLabel;

CALayer *_arrowImage;

UIActivityIndicatorView *_activityView;

    

    UIScrollView *_scrollView;

}


@property(nonatomic,unsafe_unretained) id<EGORefreshTableTailDelegate> delegate;


- (id)initWithRefreshFrame:(CGRect)frame;

- (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor;


- (void)refreshTailLastUpdatedDate;

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

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

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


- (void)setState:(EGOTailPullRefreshState)aState;


@end


@protocol EGORefreshTableTailDelegate<NSObject>

- (void)egoRefreshTableTailDidTriggerRefresh:(EGORefreshTableTailView*)view;

- (BOOL)egoRefreshTableTailDataSourceIsLoading:(EGORefreshTableTailView*)view;

@optional

- (NSDate*)egoRefreshTableTailDataSourceLastUpdated:(EGORefreshTableTailView*)view;


@end



EGORefreshTableTailView.m文件如下:

//

//  EGORefreshTableTailView.m

//  testPullAndPush

//

//  Created by liuyun on 9/4/13.

//  Copyright (c) 2013 liuyun. All rights reserved.

//


#import "EGORefreshTableTailView.h"


#define  RefreshViewHight 65.0f

#define TEXT_COLOR [UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0]

#define FLIP_ANIMATION_DURATION 0.18f


@implementation EGORefreshTableTailView


@synthesize delegate;


- (id)initWithFrame:(CGRect)frame arrowImageName:(NSString *)arrow textColor:(UIColor *)textColor  {

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

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];

        

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, RefreshViewHight - 30.0f, self.frame.size.width, 20.0f)];

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

label.font = [UIFont systemFontOfSize:12.0f];

label.textColor = textColor;

label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];

label.shadowOffset = CGSizeMake(0.0f, 1.0f);

label.backgroundColor = [UIColor clearColor];

label.textAlignment = UITextAlignmentCenter;

[self addSubview:label];

_lastUpdatedLabel=label;

label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, RefreshViewHight - 48.0f, self.frame.size.width, 20.0f)];

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

label.font = [UIFont boldSystemFontOfSize:13.0f];

label.textColor = textColor;

label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];

label.shadowOffset = CGSizeMake(0.0f, 1.0f);

label.backgroundColor = [UIColor clearColor];

label.textAlignment = UITextAlignmentCenter;

[self addSubview:label];

_statusLabel=label;

CALayer *layer = [CALayer layer];

layer.frame = CGRectMake(25.0f, 0.0f, 30.0f, 55.0f);

layer.contentsGravity = kCAGravityResizeAspect;

layer.contents = (id)[UIImage imageNamed:arrow].CGImage;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

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

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

}

#endif

[[self layer] addSublayer:layer];

_arrowImage=layer;

UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

view.frame = CGRectMake(25.0f, RefreshViewHight - 38.0f, 20.0f, 20.0f);

[self addSubview:view];

_activityView = view;

[self setState:EGOOTailPullRefreshNormal];

    }

    return self;

}


- (id)initWithRefreshFrame:(CGRect)frame  {

    return [self initWithFrame:frame arrowImageName:@"blueArrow.png" textColor:TEXT_COLOR];

}


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/


#pragma mark -

#pragma mark ScrollView Methods


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

    _scrollView = scrollView;

    CGFloat height = RefreshViewHight;

    

    DLog(@"egoRefreshScrollViewDidScroll [scrollView contentSize].height = %f, scrollView.frame.size.height = %f, self.delegate = %@", [scrollView contentSize].height, scrollView.frame.size.height, self.delegate);

    

if ([scrollView contentSize].height <= scrollView.frame.size.height) {

        [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height)];

        height += 44;

    }

    

    DLog(@"egoRefreshScrollViewDidScroll at last height = %f", height);

    

if (_state == EGOOTailPullRefreshLoading) {  

        [scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, height, 0.0f)];

} else if (scrollView.isDragging) {

DLog(@"isDragging begin");

BOOL _loading = NO;

if ([self.delegate respondsToSelector:@selector(egoRefreshTableTailDataSourceIsLoading:)]) {

_loading = [self.delegate egoRefreshTableTailDataSourceIsLoading:self];

}

        

        DLog(@"isDragging _delegate after");

if (_state == EGOOTailPullRefreshPulling && (scrollView.contentOffset.y + scrollView.frame.size.height) < scrollView.contentSize.height + RefreshViewHight && scrollView.contentOffset.y > 0.0f && !_loading) {

            DLog(@"isDragging set state normal");

            [self setState:EGOOTailPullRefreshNormal];

            DLog(@"isDragging set state normal end");

        } else if (_state == EGOOTailPullRefreshNormal && (scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height + RefreshViewHight  && !_loading) {

            DLog(@"isDragging set state pulling");

            [self setState:EGOOTailPullRefreshPulling];

            DLog(@"isDragging set state pulling end");

        }

        

        DLog(@"isDragging _delegate at end");

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

            scrollView.contentInset = UIEdgeInsetsZero;

        }

        

        DLog(@"isDragging at end");

}

}


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

_scrollView = scrollView;

BOOL _loading = NO;

if ([self.delegate respondsToSelector:@selector(egoRefreshTableTailDataSourceIsLoading:)]) {

_loading = [self.delegate egoRefreshTableTailDataSourceIsLoading:self];

}

    

    CGFloat height = RefreshViewHight;

    DLog(@"egoRefreshScrollViewDidTailEndDragging [scrollView contentSize].height = %f, scrollView.frame.size.height = %f", [scrollView contentSize].height, scrollView.frame.size.height);

    if ([scrollView contentSize].height <= scrollView.frame.size.height) {

        height += 44;

    }

    

    DLog(@"egoRefreshScrollViewDidTailEndDragging at last height = %f", height);

    

    if ((scrollView.contentOffset.y + scrollView.frame.size.height) > (scrollView.contentSize.height + RefreshViewHight) && !_loading) {

[self setState:EGOOTailPullRefreshLoading];

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.0];


        [scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, height, 0.0f)];

        

[UIView commitAnimations];

        

        if ([self.delegate respondsToSelector:@selector(egoRefreshTableTailDidTriggerRefresh:)]) {

[self.delegate egoRefreshTableTailDidTriggerRefresh:self];

}

}

}


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

_scrollView = scrollView;

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:.3];


[scrollView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];

    

[UIView commitAnimations];

[self setState:EGOOTailPullRefreshNormal];

    

}


#pragma mark -

#pragma mark Setters


- (void)refreshTailLastUpdatedDate {

if ([self.delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceLastUpdated:)]) {

NSDate *date = [self.delegate egoRefreshTableTailDataSourceLastUpdated:self];

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];

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

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

[dateFormatter setTimeStyle:NSDateFormatterShortStyle];

        

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

[[NSUserDefaults standardUserDefaults] setObject:_lastUpdatedLabel.text forKey:@"EGORefreshTableView_LastRefresh"];

[[NSUserDefaults standardUserDefaults] synchronize];

} else {

_lastUpdatedLabel.text = nil;

}

    

}


- (void)setState:(EGOTailPullRefreshState)aState

{

switch (aState) {

case EGOOTailPullRefreshPulling:

_statusLabel.text = NSLocalizedString(@"Release to refresh...", @"Release to refresh status");

[CATransaction begin];

[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];

_arrowImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);

[CATransaction commit];

break;

case EGOOTailPullRefreshNormal:

if (_state == EGOOTailPullRefreshPulling) {

[CATransaction begin];

[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];

_arrowImage.transform = CATransform3DIdentity;

[CATransaction commit];

}

_statusLabel.text = NSLocalizedString(@"Pull down to refresh...", @"Pull down to refresh status");

[_activityView stopAnimating];

[CATransaction begin];

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

_arrowImage.hidden = NO;

_arrowImage.transform = CATransform3DIdentity;

[CATransaction commit];

[self refreshTailLastUpdatedDate];

break;

case EGOOTailPullRefreshLoading:

_statusLabel.text = NSLocalizedString(@"Loading...", @"Loading Status");

[_activityView startAnimating];

[CATransaction begin];

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

_arrowImage.hidden = YES;

[CATransaction commit];

break;

default:

break;

}

_state = aState;

}


@end



你可能感兴趣的:(ios,下拉,翻页,上提)