Skip to content

Commit

Permalink
Add pull down refresh and pull up load more function.
Browse files Browse the repository at this point in the history
fix  #100
  • Loading branch information
xhzengAIB committed Jun 9, 2014
1 parent 5ab733a commit a80d382
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ - (void)viewDidLoad
for (NSInteger j = 0; j < 18; j ++) {
XHEmotion *emotion = [[XHEmotion alloc] init];
NSString *imageName = [NSString stringWithFormat:@"section%ld_emotion%ld", (long)i , (long)j % 16];
emotion.emotionPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"emotion%d@2x.gif", j] ofType:@""];
emotion.emotionPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"emotion%ld@2x.gif", (long)j] ofType:@""];
emotion.emotionConverPhoto = [UIImage imageNamed:imageName];
[emotions addObject:emotion];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

#import <UIKit/UIKit.h>
#import "XHBaseTableViewController.h"
#import "XHPullRefreshTableViewController.h"

@interface XHMessageRootViewController : XHBaseTableViewController
@interface XHMessageRootViewController : XHPullRefreshTableViewController

@end
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ - (XHPopMenu *)popMenu {
return _popMenu;
}


- (void)addContactForGroup
{
- (void)addContactForGroup {
//建立100个测试数据
NSMutableArray *items = [NSMutableArray array];
//网上拉的昵称列表,请忽视非主流。
Expand Down Expand Up @@ -188,18 +186,35 @@ - (void)addContactForGroup
[self.navigationController presentViewController:navVC animated:YES completion:nil];
}

#pragma mark - DataSource

- (void)loadDataSource {
self.isDataLoading = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *dataSource = [[NSMutableArray alloc] initWithObjects:@"华捷新闻,点击查看美女新闻呢!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点进入聊天页面,这里有多种显示样式", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", nil];
sleep(2);
dispatch_async(dispatch_get_main_queue(), ^{
self.isDataLoading = NO;
self.dataSource = dataSource;
[self.tableView reloadData];
[self endPullDownRefreshing];
});
});
}

#pragma mark - Life Cycle

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self startPullDownRefreshing];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showMenuOnView:)];

NSMutableArray *dataSource = [[NSMutableArray alloc] initWithObjects:@"华捷新闻,点击查看美女新闻呢!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点进入聊天页面,这里有多种显示样式", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", nil];
self.dataSource = dataSource;

[self.view addSubview:self.tableView];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

@interface XHPullRefreshTableViewController : XHBaseTableViewController

@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, assign) BOOL isDataLoading;

@property (nonatomic, assign) NSInteger requestCurrentPage;

- (void)startPullDownRefreshing;

- (void)endPullDownRefreshing;

- (void)endLoadMoreRefreshing;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ @interface XHPullRefreshTableViewController () <XHRefreshControlDelegate>

@implementation XHPullRefreshTableViewController

- (void)startPullDownRefreshing {
[self.refreshControl startPullDownRefreshing];
}

- (void)endPullDownRefreshing {
[self.refreshControl endPullDownRefreshing];
}

- (void)endLoadMoreRefreshing {
[self.refreshControl endLoadMoreRefresing];
}

#pragma mark - Propertys

- (XHRefreshControl *)refreshControl {
Expand All @@ -43,15 +55,26 @@ - (void)didReceiveMemoryWarning {
#pragma mark - XHRefreshControl Delegate

- (BOOL)isLoading {
return self.isLoading;
return self.isDataLoading;
}

- (void)beginPullDownRefreshing {
self.requestCurrentPage = 0;
[self loadDataSource];
}

- (void)beginLoadMoreRefreshing {
self.requestCurrentPage ++;
[self loadDataSource];
}

- (NSDate *)lastUpdateTime {
return [NSDate date];
}

- (BOOL)keepiOS7NewApiCharacter {
BOOL keeped = [[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0;
return keeped;
}

@end
6 changes: 6 additions & 0 deletions MessageDisplayKit/Classes/Views/RefreshViews/XHLoadMoreView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

#import <UIKit/UIKit.h>

#define kXHLoadMoreViewHeight 50

@interface XHLoadMoreView : UIView

@property (nonatomic, strong) UIButton *loadMoreButton;

- (void)startLoading;

- (void)endLoading;

@end
43 changes: 43 additions & 0 deletions MessageDisplayKit/Classes/Views/RefreshViews/XHLoadMoreView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,61 @@

@interface XHLoadMoreView ()

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;

@end

@implementation XHLoadMoreView

- (void)startLoading {
self.hidden = NO;
[self.loadMoreButton setTitle:@"正在载入" forState:UIControlStateNormal];
[self.activityIndicatorView startAnimating];
}

- (void)endLoading {
self.hidden = YES;
[self.loadMoreButton setTitle:@"显示下20条" forState:UIControlStateNormal];
[self.activityIndicatorView stopAnimating];
}

#pragma mark - Propertys

- (UIButton *)loadMoreButton {
if (!_loadMoreButton) {
_loadMoreButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, CGRectGetWidth(self.bounds) - 20, CGRectGetHeight(self.bounds) - 10)];
_loadMoreButton.titleLabel.font = [UIFont systemFontOfSize:16];
[_loadMoreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_loadMoreButton setBackgroundColor:[UIColor colorWithWhite:0.922 alpha:1.000]];
// _loadMoreButton.layer.borderColor = [UIColor colorWithWhite:0.867 alpha:1.000].CGColor;
// _loadMoreButton.layer.borderWidth = 1;
// _loadMoreButton.layer.shadowColor = [UIColor colorWithWhite:0.867 alpha:1.000].CGColor;
// _loadMoreButton.layer.shadowOffset = CGSizeMake(1, 1);
// _loadMoreButton.layer.shadowRadius = 1;
// _loadMoreButton.layer.shadowOpacity = 1;
}
return _loadMoreButton;
}

- (UIActivityIndicatorView *)activityIndicatorView {
if (!_activityIndicatorView) {
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicatorView.hidesWhenStopped = YES;
_activityIndicatorView.center = CGPointMake(CGRectGetWidth(self.bounds) / 3, CGRectGetHeight(self.bounds) / 2.0);
}
return _activityIndicatorView;
}

#pragma mark - Life Cycle

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.loadMoreButton];
[self addSubview:self.activityIndicatorView];
[self endLoading];
}
return self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#import <UIKit/UIKit.h>

//开始画圆圈时的offset
#define kXHRefreshCircleViewHeight 20

//圆圈开始旋转时的offset (即开始刷新数据时)
//#define HEIGHT_BEGIN_TO_REFRESH (50 + HEIGHT_BEGIN_TO_DRAW_CIRCLE)

@interface XHRefreshCircleView : UIView

//圆圈开始旋转时的offset (即开始刷新数据时)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (void)drawRect:(CGRect)rect {
CGFloat endAngle = (ABS(_offsetY) / _heightBeginToRefresh) * (M_PI * 19 / 10) + startAngle;
CGContextAddArc(context, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2, radius, startAngle, endAngle, 0);
} else {
static CGFloat startAngle = 3 * M_PI / 2;
static CGFloat startAngle = M_PI / 2;
CGFloat endAngle = (ABS(_offsetY) / _heightBeginToRefresh) * (M_PI * 19 / 10) + startAngle;
CGContextAddArc(context, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2, radius, startAngle, endAngle, 0);
}
Expand Down
22 changes: 11 additions & 11 deletions MessageDisplayKit/Classes/Views/RefreshViews/XHRefreshControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// 1、是否加载中
// 2、将要开始下拉刷新的方法
// 3、将要开始上提加载更多的方法
// 4、最后更新数据的时间

// 可选实现的delegate
// 1、是否支持下拉刷新
// 2、是否支持上提加载更多
// 3、标识下拉刷新是UIScrollView的子view,还是UIScrollView父view的子view
// 4、是否保留iOS7的新特性,只对滚动视图有效
// 5、多少次上提加载后,启用点击按钮进行加载一页数据
// 6、需要多长的下拉UIScrollView最大高度限定值

typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
XHRefreshViewLayerTypeOnScrollView = 0,
Expand All @@ -47,18 +47,25 @@ typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
*/
- (void)beginLoadMoreRefreshing;

/**
* 4、最后更新数据的时间
*
* @return 返回缓存最后更新某个页面的时间
*/
- (NSDate *)lastUpdateTime;

@optional
/**
* 1、是否支持下拉刷新
*
* @return 如果没有实现该delegate方法,默认是不支持下拉的,为NO
* @return 如果没有实现该delegate方法,默认是支持下拉的,为YES
*/
- (BOOL)isPullDownRefreshed;

/**
* 2、是否支持上提加载更多
*
* @return 如果没有实现该delegate方法,默认是不支持上提加载更多的,为NO
* @return 如果没有实现该delegate方法,默认是支持上提加载更多的,为YES
*/
- (BOOL)isLoadMoreRefreshed;

Expand All @@ -72,7 +79,7 @@ typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
/**
* 4、UIScrollView的控制器是否保留iOS7新的特性,意思是:tablView的内容是否可以穿透过导航条
*
* @return 如果不是先该delegate方法,默认是支持的
* @return 如果不是先该delegate方法,默认是不支持的
*/
- (BOOL)keepiOS7NewApiCharacter;

Expand All @@ -83,13 +90,6 @@ typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
*/
- (NSInteger)autoLoadMoreRefreshedCountConverManual;

/**
* 6、需要多长的下拉UIScrollView最大高度限定值
*
* @return 如果不实现该delegate方法,默认是64个像素高度
*/
- (CGFloat)pullDownRefreshTotalPixels;

@end

@interface XHRefreshControl : NSObject
Expand Down
Loading

0 comments on commit a80d382

Please sign in to comment.