| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- //
- // ScrollRefreshViewController.m
- //
- // @author Shiki
- //
- #import "ScrollRefreshViewController.h"
- #import "UIView+Layout.h"
- @interface ScrollRefreshViewController ()
- {
- __weak UIScrollView *_refreshScrollView;
- }
- @end
- @implementation ScrollRefreshViewController
- - (void)initialize
- {
- _canRefresh = YES;
- _canLoadMore = YES;
- }
- - (instancetype)init
- {
- if (self = [super init])
- {
- [self initialize];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder
- {
- if ((self = [super initWithCoder:aDecoder]))
- {
- [self initialize];
- }
- return self;
- }
- - (void)addRefreshScrollView
- {
-
- }
- - (void)addOwnViews
- {
- [self addNoDataView];
-
- [self addRefreshScrollView];
-
- [self addHeaderView];
-
- [self addFooterView];
-
-
- }
- - (void)addHeaderView
- {
- }
- - (void)addFooterView
- {
-
- }
- - (void)layoutOnIPhone
- {
- [self layoutRefreshScrollView];
- [self layoutHeaderRefreshView];
- [self layoutFooterRefreshView];
- }
- - (void)layoutRefreshScrollView
- {
- _refreshScrollView.frame = self.view.bounds;
- _noDataView.frame = _refreshScrollView.frame;
- }
- - (void)layoutHeaderRefreshView
- {
- if (_headerView)
- {
- CGRect rect = _refreshScrollView.bounds;
- [_headerView setFrameAndLayout:CGRectMake(0, -rect.size.height, rect.size.width, rect.size.height)];
- }
- }
- - (void)layoutFooterRefreshView
- {
- if (_footerView)
- {
- CGRect rect = _refreshScrollView.bounds;
- [_footerView setFrameAndLayout:CGRectMake(0, _refreshScrollView.contentSize.height, rect.size.width, rect.size.height)];
-
- if (_canLoadMore)
- {
- [self setFooterViewVisibility:_refreshScrollView.contentSize.height >= rect.size.height];
- }
- }
- }
- #pragma mark - Pull to Refresh
- - (void)setHeaderView:(UIView<RefreshAbleView> *)aView
- {
- if (!_refreshScrollView)
- {
- return;
- }
-
-
- if (_headerView && [_headerView isDescendantOfView:_refreshScrollView])
- {
- [_headerView removeFromSuperview];
- }
- _headerView = nil;
-
- if (aView)
- {
- _headerView = aView;
- [_refreshScrollView addSubview:_headerView];
- }
- }
- - (CGFloat)headerRefreshHeight
- {
- return [_headerView refreshHeight];
- // if (_headView)
- // {
- // return _headView.frame.size.height;
- // }
- // else
- // {
- // return kDefaultRefreshHeightOffset;
- // }
- }
- - (void)pinHeaderAndRefresh
- {
- [self pinHeaderView];
- [self refresh];
- }
- - (void)pinHeaderView
- {
- [UIView animateWithDuration:0.3 animations:^(void) {
- _refreshScrollView.contentInset = UIEdgeInsetsMake([self headerRefreshHeight], 0, 0, 0);
- [_headerView loading];
- }];
- }
- - (void)unpinHeaderView
- {
- [UIView animateWithDuration:0.3 animations:^(void){
- _refreshScrollView.contentInset = UIEdgeInsetsZero;
- [_headerView loadingOver];
- } completion:^(BOOL finished) {
- [self layoutFooterRefreshView];
- }];
- }
- - (void)willBeginRefresh
- {
- if (_canRefresh)
- {
- [self pinHeaderView];
- }
- }
- - (BOOL)refresh
- {
- if (_isRefreshing || _isLoadingMore)
- {
- return NO;
- }
-
- [self willBeginRefresh];
- _isRefreshing = YES;
-
- [self onRefresh];
- return YES;
- }
- - (void)onRefresh
- {
-
- }
- - (void)refreshCompleted
- {
- _isRefreshing = NO;
-
- if (_canRefresh)
- {
- [self unpinHeaderView];
- }
- }
- #pragma mark - Load More
- - (void)setFooterView:(UIView<RefreshAbleView> *)aView
- {
- if (!_refreshScrollView)
- {
- return;
- }
-
-
- if (_footerView && [_footerView isDescendantOfView:_refreshScrollView])
- {
- [_footerView removeFromSuperview];
- }
- _footerView = nil;
-
- if (aView)
- {
- _footerView = aView;
- [_refreshScrollView addSubview:_footerView];
- }
- }
- - (void)willBeginLoadingMore
- {
- [UIView animateWithDuration:0.3 animations:^(void) {
- _refreshScrollView.contentInset = UIEdgeInsetsMake(0, 0, [self footerLoadMoreHeight], 0);
- }];
- }
- - (void)loadMoreCompleted
- {
- _isLoadingMore = NO;
-
- [UIView animateWithDuration:0.3 animations:^(void) {
- _refreshScrollView.contentInset = UIEdgeInsetsZero;
- [_footerView loadingOver];
- } completion:^(BOOL finished) {
- [self layoutFooterRefreshView];
- }];
-
-
-
- }
- - (BOOL)loadMore
- {
- if (_isRefreshing || _isLoadingMore)
- {
- return NO;
- }
-
- [self willBeginLoadingMore];
- _isLoadingMore = YES;
-
- [self onLoadMore];
- return YES;
- }
- - (void)onLoadMore
- {
-
- }
- - (CGFloat)footerLoadMoreHeight
- {
- return [_footerView refreshHeight];
- // if (_footerView)
- // {
- // return _footerView.frame.size.height;
- // }
- // else
- // {
- // return kDefaultRefreshHeightOffset;
- // }
- }
- - (void)setFooterViewVisibility:(BOOL)visible
- {
- _footerView.hidden = !visible;
-
- // if (visible && _refreshScrollView.tableFooterView != _footerView)
- // {
- // _refreshScrollView.tableFooterView = _footerView;
- // }
- // else if (!visible)
- // {
- // _refreshScrollView.tableFooterView = nil;
- // }
- }
- #pragma mark -
- - (void)allLoadingCompleted
- {
- if (_isRefreshing)
- {
- [self refreshCompleted];
- }
-
- if (_isLoadingMore)
- {
- [self loadMoreCompleted];
- }
-
-
- }
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
- if (_isRefreshing)
- {
- return;
- }
-
- _isDragging = YES;
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- if (_headerView && !_isRefreshing && _isDragging && scrollView.contentOffset.y < 0)
- {
- CGFloat headerHeight = [self headerRefreshHeight];
-
- BOOL releaseLoading = scrollView.contentOffset.y < -headerHeight;
- if (releaseLoading)
- {
- [_headerView releaseLoading];
- }
- else
- {
- CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
- offset = MIN(offset, headerHeight);
- scrollView.contentInset = UIEdgeInsetsMake(offset, 0, 0, 0);
- [_headerView willLoading];
- }
- }
- else if (_footerView && !_footerView.hidden && !_isLoadingMore && _canLoadMore)
- {
- if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height)
- {
- CGFloat footRefreshHeight = [self footerLoadMoreHeight];
- if ( scrollView.contentOffset.y + scrollView.frame.size.height < scrollView.contentSize.height + footRefreshHeight)
- {
- CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);
- offset = MIN(offset, footRefreshHeight);
- scrollView.contentInset = UIEdgeInsetsMake(0, 0, offset, 0);
-
- [_footerView willLoading];
-
- }
- else
- {
- [_footerView releaseLoading];
- }
- }
-
-
- }
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
- if (_isRefreshing)
- {
- return;
- }
-
- _isDragging = NO;
-
-
- if (scrollView.contentOffset.y < 0)
- {
- if (_canRefresh)
- {
- if (_headerView && scrollView.contentOffset.y <= - [self headerRefreshHeight])
- {
- BOOL ref = [self refresh];
- if (ref)
- {
- [_headerView loading];
- }
- }
- else
- {
- [self refreshCompleted];
- }
- }
- }
- else
- {
- if (_canLoadMore)
- {
- if (_footerView && !_footerView.hidden && scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height + [self footerLoadMoreHeight])
- {
- if ([self loadMore])
- {
- [_footerView loading];
- }
- }
- else
- {
- [self loadMoreCompleted];
- }
- }
-
- }
- }
- #pragma mark - UITableViewDelegate
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return nil;
- }
- - (void)reloadData
- {
-
- }
- - (void)showNoDataView
- {
-
- }
- - (void)addNoDataView
- {
-
- }
- - (BOOL)hasData
- {
- return YES;
- }
- @end
|