Skip to content

Commit a80d382

Browse files
committed
Add pull down refresh and pull up load more function.
fix xhzengAIB#100
1 parent 5ab733a commit a80d382

13 files changed

Lines changed: 461 additions & 40 deletions

File tree

Example/MessageDisplayExample/MessageDisplayExample/XHDemoWeChatMessageTableViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ - (void)viewDidLoad
149149
for (NSInteger j = 0; j < 18; j ++) {
150150
XHEmotion *emotion = [[XHEmotion alloc] init];
151151
NSString *imageName = [NSString stringWithFormat:@"section%ld_emotion%ld", (long)i , (long)j % 16];
152-
emotion.emotionPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"emotion%d@2x.gif", j] ofType:@""];
152+
emotion.emotionPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"emotion%ld@2x.gif", (long)j] ofType:@""];
153153
emotion.emotionConverPhoto = [UIImage imageNamed:imageName];
154154
[emotions addObject:emotion];
155155
}

Example/MessageDisplayExample/MessageDisplayExample/XHMessageRootViewController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//
88

99
#import <UIKit/UIKit.h>
10-
#import "XHBaseTableViewController.h"
10+
#import "XHPullRefreshTableViewController.h"
1111

12-
@interface XHMessageRootViewController : XHBaseTableViewController
12+
@interface XHMessageRootViewController : XHPullRefreshTableViewController
1313

1414
@end

Example/MessageDisplayExample/MessageDisplayExample/XHMessageRootViewController.m

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ - (XHPopMenu *)popMenu {
104104
return _popMenu;
105105
}
106106

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

189+
#pragma mark - DataSource
190+
191+
- (void)loadDataSource {
192+
self.isDataLoading = YES;
193+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
194+
NSMutableArray *dataSource = [[NSMutableArray alloc] initWithObjects:@"华捷新闻,点击查看美女新闻呢!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点进入聊天页面,这里有多种显示样式", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"请问你现在在哪里啊?我在广州天河", @"点击我查看最新消息,里面有惊喜哦!", @"点击我查看最新消息,里面有惊喜哦!", nil];
195+
sleep(2);
196+
dispatch_async(dispatch_get_main_queue(), ^{
197+
self.isDataLoading = NO;
198+
self.dataSource = dataSource;
199+
[self.tableView reloadData];
200+
[self endPullDownRefreshing];
201+
});
202+
});
203+
}
191204

192205
#pragma mark - Life Cycle
193206

207+
- (void)viewDidAppear:(BOOL)animated {
208+
[super viewDidAppear:animated];
209+
[self startPullDownRefreshing];
210+
}
211+
194212
- (void)viewDidLoad {
195213
[super viewDidLoad];
196214
// Do any additional setup after loading the view.
197215

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

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

MessageDisplayKit/Classes/Controllers/XHBaseTableViewController/XHPullRefreshTableViewController.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010

1111
@interface XHPullRefreshTableViewController : XHBaseTableViewController
1212

13-
@property (nonatomic, assign) BOOL isLoading;
13+
@property (nonatomic, assign) BOOL isDataLoading;
1414

1515
@property (nonatomic, assign) NSInteger requestCurrentPage;
1616

17+
- (void)startPullDownRefreshing;
18+
19+
- (void)endPullDownRefreshing;
20+
21+
- (void)endLoadMoreRefreshing;
22+
1723
@end

MessageDisplayKit/Classes/Controllers/XHBaseTableViewController/XHPullRefreshTableViewController.m

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ @interface XHPullRefreshTableViewController () <XHRefreshControlDelegate>
1818

1919
@implementation XHPullRefreshTableViewController
2020

21+
- (void)startPullDownRefreshing {
22+
[self.refreshControl startPullDownRefreshing];
23+
}
24+
25+
- (void)endPullDownRefreshing {
26+
[self.refreshControl endPullDownRefreshing];
27+
}
28+
29+
- (void)endLoadMoreRefreshing {
30+
[self.refreshControl endLoadMoreRefresing];
31+
}
32+
2133
#pragma mark - Propertys
2234

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

4557
- (BOOL)isLoading {
46-
return self.isLoading;
58+
return self.isDataLoading;
4759
}
4860

4961
- (void)beginPullDownRefreshing {
5062
self.requestCurrentPage = 0;
63+
[self loadDataSource];
5164
}
5265

5366
- (void)beginLoadMoreRefreshing {
5467
self.requestCurrentPage ++;
68+
[self loadDataSource];
69+
}
70+
71+
- (NSDate *)lastUpdateTime {
72+
return [NSDate date];
73+
}
74+
75+
- (BOOL)keepiOS7NewApiCharacter {
76+
BOOL keeped = [[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0;
77+
return keeped;
5578
}
5679

5780
@end

MessageDisplayKit/Classes/Views/RefreshViews/XHLoadMoreView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88

99
#import <UIKit/UIKit.h>
1010

11+
#define kXHLoadMoreViewHeight 50
12+
1113
@interface XHLoadMoreView : UIView
1214

1315
@property (nonatomic, strong) UIButton *loadMoreButton;
1416

17+
- (void)startLoading;
18+
19+
- (void)endLoading;
20+
1521
@end

MessageDisplayKit/Classes/Views/RefreshViews/XHLoadMoreView.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,61 @@
1010

1111
@interface XHLoadMoreView ()
1212

13+
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
14+
1315
@end
1416

1517
@implementation XHLoadMoreView
1618

19+
- (void)startLoading {
20+
self.hidden = NO;
21+
[self.loadMoreButton setTitle:@"正在载入" forState:UIControlStateNormal];
22+
[self.activityIndicatorView startAnimating];
23+
}
24+
25+
- (void)endLoading {
26+
self.hidden = YES;
27+
[self.loadMoreButton setTitle:@"显示下20条" forState:UIControlStateNormal];
28+
[self.activityIndicatorView stopAnimating];
29+
}
30+
1731
#pragma mark - Propertys
1832

33+
- (UIButton *)loadMoreButton {
34+
if (!_loadMoreButton) {
35+
_loadMoreButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, CGRectGetWidth(self.bounds) - 20, CGRectGetHeight(self.bounds) - 10)];
36+
_loadMoreButton.titleLabel.font = [UIFont systemFontOfSize:16];
37+
[_loadMoreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
38+
[_loadMoreButton setBackgroundColor:[UIColor colorWithWhite:0.922 alpha:1.000]];
39+
// _loadMoreButton.layer.borderColor = [UIColor colorWithWhite:0.867 alpha:1.000].CGColor;
40+
// _loadMoreButton.layer.borderWidth = 1;
41+
// _loadMoreButton.layer.shadowColor = [UIColor colorWithWhite:0.867 alpha:1.000].CGColor;
42+
// _loadMoreButton.layer.shadowOffset = CGSizeMake(1, 1);
43+
// _loadMoreButton.layer.shadowRadius = 1;
44+
// _loadMoreButton.layer.shadowOpacity = 1;
45+
}
46+
return _loadMoreButton;
47+
}
48+
49+
- (UIActivityIndicatorView *)activityIndicatorView {
50+
if (!_activityIndicatorView) {
51+
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
52+
_activityIndicatorView.hidesWhenStopped = YES;
53+
_activityIndicatorView.center = CGPointMake(CGRectGetWidth(self.bounds) / 3, CGRectGetHeight(self.bounds) / 2.0);
54+
}
55+
return _activityIndicatorView;
56+
}
57+
1958
#pragma mark - Life Cycle
2059

2160
- (id)initWithFrame:(CGRect)frame {
2261
self = [super initWithFrame:frame];
2362
if (self) {
2463
// Initialization code
64+
self.backgroundColor = [UIColor whiteColor];
65+
[self addSubview:self.loadMoreButton];
66+
[self addSubview:self.activityIndicatorView];
67+
[self endLoading];
2568
}
2669
return self;
2770
}

MessageDisplayKit/Classes/Views/RefreshViews/XHRefreshCircleView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#import <UIKit/UIKit.h>
1010

11+
//开始画圆圈时的offset
12+
#define kXHRefreshCircleViewHeight 20
13+
14+
//圆圈开始旋转时的offset (即开始刷新数据时)
15+
//#define HEIGHT_BEGIN_TO_REFRESH (50 + HEIGHT_BEGIN_TO_DRAW_CIRCLE)
16+
1117
@interface XHRefreshCircleView : UIView
1218

1319
//圆圈开始旋转时的offset (即开始刷新数据时)

MessageDisplayKit/Classes/Views/RefreshViews/XHRefreshCircleView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ - (void)drawRect:(CGRect)rect {
4747
CGFloat endAngle = (ABS(_offsetY) / _heightBeginToRefresh) * (M_PI * 19 / 10) + startAngle;
4848
CGContextAddArc(context, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2, radius, startAngle, endAngle, 0);
4949
} else {
50-
static CGFloat startAngle = 3 * M_PI / 2;
50+
static CGFloat startAngle = M_PI / 2;
5151
CGFloat endAngle = (ABS(_offsetY) / _heightBeginToRefresh) * (M_PI * 19 / 10) + startAngle;
5252
CGContextAddArc(context, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2, radius, startAngle, endAngle, 0);
5353
}

MessageDisplayKit/Classes/Views/RefreshViews/XHRefreshControl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
// 1、是否加载中
1414
// 2、将要开始下拉刷新的方法
1515
// 3、将要开始上提加载更多的方法
16+
// 4、最后更新数据的时间
1617

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

2525
typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
2626
XHRefreshViewLayerTypeOnScrollView = 0,
@@ -47,18 +47,25 @@ typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
4747
*/
4848
- (void)beginLoadMoreRefreshing;
4949

50+
/**
51+
* 4、最后更新数据的时间
52+
*
53+
* @return 返回缓存最后更新某个页面的时间
54+
*/
55+
- (NSDate *)lastUpdateTime;
56+
5057
@optional
5158
/**
5259
* 1、是否支持下拉刷新
5360
*
54-
* @return 如果没有实现该delegate方法,默认是不支持下拉的,为NO
61+
* @return 如果没有实现该delegate方法,默认是支持下拉的,为YES
5562
*/
5663
- (BOOL)isPullDownRefreshed;
5764

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

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

@@ -83,13 +90,6 @@ typedef NS_ENUM(NSInteger, XHRefreshViewLayerType) {
8390
*/
8491
- (NSInteger)autoLoadMoreRefreshedCountConverManual;
8592

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

9595
@interface XHRefreshControl : NSObject

0 commit comments

Comments
 (0)