`
renzhelife
  • 浏览: 668074 次
文章分类
社区版块
存档分类
最新评论

UIScrollView UITableView 上拉 上提 刷新 代码

 
阅读更多
demo(代码)下载地址 :http://www.cocoachina.com/bbs/job.php?action=download&aid=26207

下面是.h文件的代码


//
//EGORefreshTableHeaderView.h
//Demo
//
//Created by Devin Doty on 10/14/09October14.
//Copyright 2009 enormego. All rights reserved.
//
//修改人:禚来强 iphone开发:79190809 邮箱:zhuolaiqiang@gmail.com
//原文地址:http://blog.csdn.net/diyagoanyhacker/archive/2011/05/24/6441805.aspx


#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

typedef enum{
EGOOPullRefreshPulling = 0,
EGOOPullRefreshNormal,
EGOOPullRefreshLoading,
} EGOPullRefreshState;

@protocol EGORefreshTableHeaderDelegate;
@interface EGORefreshTableHeaderView : UIView {

id _delegate;
EGOPullRefreshState _state;

UILabel *_lastUpdatedLabel;
UILabel *_statusLabel;
CALayer *_arrowImage;
UIActivityIndicatorView *_activityView;


}

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

- (void)refreshLastUpdatedDate;
- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView;
- (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView;
- (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView;

@end
@protocol EGORefreshTableHeaderDelegate
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view;
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view;
@optional
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view;
@end

下面是.m文件的代码

//
//EGORefreshTableHeaderView.m
//Demo
//
//修改人:禚来强 iphone开发:79190809 邮箱:zhuolaiqiang@gmail.com
//


#defineRefreshViewHight 65.0f

#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 EGORefreshTableHeaderView (Private)
- (void)setState:(EGOPullRefreshState)aState;
@end

@implementation EGORefreshTableHeaderView

@synthesize delegate=_delegate;


- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame: frame];
if (self) {

self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
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 = TEXT_COLOR;
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, RefreshViewHight - 48.0f, self.frame.size.width, 20.0f)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.font = [UIFont boldSystemFontOfSize:13.0f];
label.textColor = TEXT_COLOR;
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, RefreshViewHight - RefreshViewHight, 30.0f, 55.0f);
layer.contentsGravity = kCAGravityResizeAspect;
layer.contents = (id)[UIImage imageNamed:@"blueArrow.png"].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;
[view release];


[self setState:EGOOPullRefreshNormal];

}

return self;

}

//
//EGORefreshTableHeaderView.h
//Demo
//
//Created by Devin Doty on 10/14/09October14.
//Copyright 2009 enormego. All rights reserved.
//
//修改人:禚来强 iphone开发:79190809 邮箱:zhuolaiqiang@gmail.com
//原文地址:http://blog.csdn.net/diyagoanyhacker/archive/2011/05/24/6441805.aspx
#pragma mark -
#pragma mark Setters

- (void)refreshLastUpdatedDate {

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

NSDate *date = [_delegate egoRefreshTableHeaderDataSourceLastUpdated:self];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setAMSymbol:@"上午"];
[formatter setPMSymbol:@"下午"];
[formatter setDateFormat:@"yyyy/MM/dd hh:mm:a"];
_lastUpdatedLabel.text = [NSString stringWithFormat:@"最后更新: %@", [formatter stringFromDate:date]];

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

} else {

_lastUpdatedLabel.text = nil;

}

}

- (void)setState:(EGOPullRefreshState)aState{

switch (aState) {
case EGOOPullRefreshPulling:

_statusLabel.text = NSLocalizedString(@"松开即可更新...", @"松开即可更新...");
[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(@"上拉即可更新...", @"上拉即可更新...");
[_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(@"加载中...", @"加载中...");
[_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(0.0, 0.0f, RefreshViewHight, 0.0f);

} else if (scrollView.isDragging) {

BOOL _loading = NO;
if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceIsLoading:)]) {
_loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];
}

if (_state == EGOOPullRefreshPulling && scrollView.contentOffset.y + (scrollView.frame.size.height) < scrollView.contentSize.height + RefreshViewHight && scrollView.contentOffset.y > 0.0f && !_loading) {
[self setState:EGOOPullRefreshNormal];
} else if (_state == EGOOPullRefreshNormal && scrollView.contentOffset.y + (scrollView.frame.size.height) > scrollView.contentSize.height + RefreshViewHight&& !_loading) {
[self setState:EGOOPullRefreshPulling];
}

if (scrollView.contentInset.bottom != 0) {
scrollView.contentInset = UIEdgeInsetsZero;
}

}

}

//当用户停止拖动,并且手指从屏幕中拿开的的时候调用此方法
- (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView {

BOOL _loading = NO;
if ([_delegate respondsToSelector:@selector(egoRefreshTableHeaderDataSourceIsLoading:)]) {
_loading = [_delegate egoRefreshTableHeaderDataSourceIsLoading:self];
}

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

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

[self setState:EGOOPullRefreshLoading];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, RefreshViewHight, 0.0f);
[UIView commitAnimations];

}

}

//当开发者页面页面刷新完毕调用此方法,[delegate egoRefreshScrollViewDataSourceDidFinishedLoading: scrollView];
- (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


下面是调用类的代码


//
//RootViewController.m
//TableViewPull
//
//Created by Devin Doty on 10/16/09October16.
//Copyright enormego 2009. All rights reserved.
//
//修改:禚来强 email:zhuolaiqiang@gmail.com

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self.tableView reloadData];

//self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 320, 480);

if (_refreshHeaderView == nil) {

EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame: CGRectMake(0.0f, self.tableView.contentSize.height, 320, 650)];
NSLog(@"%@", NSStringFromCGRect( view.frame ));
view.delegate = self;
[self.tableView addSubview:view];
_refreshHeaderView = view;
[view release];

}

//update the last update date
[_refreshHeaderView refreshLastUpdatedDate];


}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}


#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.

return cell;
}

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

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

}


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

}

- (void)doneLoadingTableViewData{

//model should call this when its done loading
_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:(EGORefreshTableHeaderView*)view{

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

}

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

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

}

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

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

}


#pragma mark -
#pragma mark Memory Management

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
_refreshHeaderView=nil;
}

- (void)dealloc {

_refreshHeaderView = nil;
[super dealloc];
}


@end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics