ScrollRefreshViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // ScrollRefreshViewController.m
  3. //
  4. // @author Shiki
  5. //
  6. #import "ScrollRefreshViewController.h"
  7. #import "UIView+Layout.h"
  8. @interface ScrollRefreshViewController ()
  9. {
  10. __weak UIScrollView *_refreshScrollView;
  11. }
  12. @end
  13. @implementation ScrollRefreshViewController
  14. - (void)initialize
  15. {
  16. _canRefresh = YES;
  17. _canLoadMore = YES;
  18. }
  19. - (instancetype)init
  20. {
  21. if (self = [super init])
  22. {
  23. [self initialize];
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  28. {
  29. if ((self = [super initWithCoder:aDecoder]))
  30. {
  31. [self initialize];
  32. }
  33. return self;
  34. }
  35. - (void)addRefreshScrollView
  36. {
  37. }
  38. - (void)addOwnViews
  39. {
  40. [self addNoDataView];
  41. [self addRefreshScrollView];
  42. [self addHeaderView];
  43. [self addFooterView];
  44. }
  45. - (void)addHeaderView
  46. {
  47. }
  48. - (void)addFooterView
  49. {
  50. }
  51. - (void)layoutOnIPhone
  52. {
  53. [self layoutRefreshScrollView];
  54. [self layoutHeaderRefreshView];
  55. [self layoutFooterRefreshView];
  56. }
  57. - (void)layoutRefreshScrollView
  58. {
  59. _refreshScrollView.frame = self.view.bounds;
  60. _noDataView.frame = _refreshScrollView.frame;
  61. }
  62. - (void)layoutHeaderRefreshView
  63. {
  64. if (_headerView)
  65. {
  66. CGRect rect = _refreshScrollView.bounds;
  67. [_headerView setFrameAndLayout:CGRectMake(0, -rect.size.height, rect.size.width, rect.size.height)];
  68. }
  69. }
  70. - (void)layoutFooterRefreshView
  71. {
  72. if (_footerView)
  73. {
  74. CGRect rect = _refreshScrollView.bounds;
  75. [_footerView setFrameAndLayout:CGRectMake(0, _refreshScrollView.contentSize.height, rect.size.width, rect.size.height)];
  76. if (_canLoadMore)
  77. {
  78. [self setFooterViewVisibility:_refreshScrollView.contentSize.height >= rect.size.height];
  79. }
  80. }
  81. }
  82. #pragma mark - Pull to Refresh
  83. - (void)setHeaderView:(UIView<RefreshAbleView> *)aView
  84. {
  85. if (!_refreshScrollView)
  86. {
  87. return;
  88. }
  89. if (_headerView && [_headerView isDescendantOfView:_refreshScrollView])
  90. {
  91. [_headerView removeFromSuperview];
  92. }
  93. _headerView = nil;
  94. if (aView)
  95. {
  96. _headerView = aView;
  97. [_refreshScrollView addSubview:_headerView];
  98. }
  99. }
  100. - (CGFloat)headerRefreshHeight
  101. {
  102. return [_headerView refreshHeight];
  103. // if (_headView)
  104. // {
  105. // return _headView.frame.size.height;
  106. // }
  107. // else
  108. // {
  109. // return kDefaultRefreshHeightOffset;
  110. // }
  111. }
  112. - (void)pinHeaderAndRefresh
  113. {
  114. [self pinHeaderView];
  115. [self refresh];
  116. }
  117. - (void)pinHeaderView
  118. {
  119. [UIView animateWithDuration:0.3 animations:^(void) {
  120. _refreshScrollView.contentInset = UIEdgeInsetsMake([self headerRefreshHeight], 0, 0, 0);
  121. [_headerView loading];
  122. }];
  123. }
  124. - (void)unpinHeaderView
  125. {
  126. [UIView animateWithDuration:0.3 animations:^(void){
  127. _refreshScrollView.contentInset = UIEdgeInsetsZero;
  128. [_headerView loadingOver];
  129. } completion:^(BOOL finished) {
  130. [self layoutFooterRefreshView];
  131. }];
  132. }
  133. - (void)willBeginRefresh
  134. {
  135. if (_canRefresh)
  136. {
  137. [self pinHeaderView];
  138. }
  139. }
  140. - (BOOL)refresh
  141. {
  142. if (_isRefreshing || _isLoadingMore)
  143. {
  144. return NO;
  145. }
  146. [self willBeginRefresh];
  147. _isRefreshing = YES;
  148. [self onRefresh];
  149. return YES;
  150. }
  151. - (void)onRefresh
  152. {
  153. }
  154. - (void)refreshCompleted
  155. {
  156. _isRefreshing = NO;
  157. if (_canRefresh)
  158. {
  159. [self unpinHeaderView];
  160. }
  161. }
  162. #pragma mark - Load More
  163. - (void)setFooterView:(UIView<RefreshAbleView> *)aView
  164. {
  165. if (!_refreshScrollView)
  166. {
  167. return;
  168. }
  169. if (_footerView && [_footerView isDescendantOfView:_refreshScrollView])
  170. {
  171. [_footerView removeFromSuperview];
  172. }
  173. _footerView = nil;
  174. if (aView)
  175. {
  176. _footerView = aView;
  177. [_refreshScrollView addSubview:_footerView];
  178. }
  179. }
  180. - (void)willBeginLoadingMore
  181. {
  182. [UIView animateWithDuration:0.3 animations:^(void) {
  183. _refreshScrollView.contentInset = UIEdgeInsetsMake(0, 0, [self footerLoadMoreHeight], 0);
  184. }];
  185. }
  186. - (void)loadMoreCompleted
  187. {
  188. _isLoadingMore = NO;
  189. [UIView animateWithDuration:0.3 animations:^(void) {
  190. _refreshScrollView.contentInset = UIEdgeInsetsZero;
  191. [_footerView loadingOver];
  192. } completion:^(BOOL finished) {
  193. [self layoutFooterRefreshView];
  194. }];
  195. }
  196. - (BOOL)loadMore
  197. {
  198. if (_isRefreshing || _isLoadingMore)
  199. {
  200. return NO;
  201. }
  202. [self willBeginLoadingMore];
  203. _isLoadingMore = YES;
  204. [self onLoadMore];
  205. return YES;
  206. }
  207. - (void)onLoadMore
  208. {
  209. }
  210. - (CGFloat)footerLoadMoreHeight
  211. {
  212. return [_footerView refreshHeight];
  213. // if (_footerView)
  214. // {
  215. // return _footerView.frame.size.height;
  216. // }
  217. // else
  218. // {
  219. // return kDefaultRefreshHeightOffset;
  220. // }
  221. }
  222. - (void)setFooterViewVisibility:(BOOL)visible
  223. {
  224. _footerView.hidden = !visible;
  225. // if (visible && _refreshScrollView.tableFooterView != _footerView)
  226. // {
  227. // _refreshScrollView.tableFooterView = _footerView;
  228. // }
  229. // else if (!visible)
  230. // {
  231. // _refreshScrollView.tableFooterView = nil;
  232. // }
  233. }
  234. #pragma mark -
  235. - (void)allLoadingCompleted
  236. {
  237. if (_isRefreshing)
  238. {
  239. [self refreshCompleted];
  240. }
  241. if (_isLoadingMore)
  242. {
  243. [self loadMoreCompleted];
  244. }
  245. }
  246. #pragma mark - UIScrollViewDelegate
  247. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  248. {
  249. if (_isRefreshing)
  250. {
  251. return;
  252. }
  253. _isDragging = YES;
  254. }
  255. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  256. {
  257. if (_headerView && !_isRefreshing && _isDragging && scrollView.contentOffset.y < 0)
  258. {
  259. CGFloat headerHeight = [self headerRefreshHeight];
  260. BOOL releaseLoading = scrollView.contentOffset.y < -headerHeight;
  261. if (releaseLoading)
  262. {
  263. [_headerView releaseLoading];
  264. }
  265. else
  266. {
  267. CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
  268. offset = MIN(offset, headerHeight);
  269. scrollView.contentInset = UIEdgeInsetsMake(offset, 0, 0, 0);
  270. [_headerView willLoading];
  271. }
  272. }
  273. else if (_footerView && !_footerView.hidden && !_isLoadingMore && _canLoadMore)
  274. {
  275. if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height)
  276. {
  277. CGFloat footRefreshHeight = [self footerLoadMoreHeight];
  278. if ( scrollView.contentOffset.y + scrollView.frame.size.height < scrollView.contentSize.height + footRefreshHeight)
  279. {
  280. CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
  281. offset = MIN(offset, footRefreshHeight);
  282. scrollView.contentInset = UIEdgeInsetsMake(0, 0, offset, 0);
  283. [_footerView willLoading];
  284. }
  285. else
  286. {
  287. [_footerView releaseLoading];
  288. }
  289. }
  290. }
  291. }
  292. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  293. {
  294. if (_isRefreshing)
  295. {
  296. return;
  297. }
  298. _isDragging = NO;
  299. if (scrollView.contentOffset.y < 0)
  300. {
  301. if (_canRefresh)
  302. {
  303. if (_headerView && scrollView.contentOffset.y <= - [self headerRefreshHeight])
  304. {
  305. BOOL ref = [self refresh];
  306. if (ref)
  307. {
  308. [_headerView loading];
  309. }
  310. }
  311. else
  312. {
  313. [self refreshCompleted];
  314. }
  315. }
  316. }
  317. else
  318. {
  319. if (_canLoadMore)
  320. {
  321. if (_footerView && !_footerView.hidden && scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height + [self footerLoadMoreHeight])
  322. {
  323. if ([self loadMore])
  324. {
  325. [_footerView loading];
  326. }
  327. }
  328. else
  329. {
  330. [self loadMoreCompleted];
  331. }
  332. }
  333. }
  334. }
  335. #pragma mark - UITableViewDelegate
  336. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  337. {
  338. return 0;
  339. }
  340. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  341. {
  342. return nil;
  343. }
  344. - (void)reloadData
  345. {
  346. }
  347. - (void)showNoDataView
  348. {
  349. }
  350. - (void)addNoDataView
  351. {
  352. }
  353. - (BOOL)hasData
  354. {
  355. return YES;
  356. }
  357. @end