澎湃iPad上线版本

TPCollectionController.m 16KB

    // // TPCollectionController.m // ThePaperHD // // Created by scar1900 on 15/1/6. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "TPCollectionController.h" @implementation collectLoadView @synthesize state = _state; @synthesize delegate; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self addSubview:self.activityView]; [self addSubview:self.textLabel]; self.state = RefreshStateNormal; } return self; } - (UIActivityIndicatorView*)activityView { if (!_activityView) { _activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; _activityView.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-25/2, 10, 25, 25); [_activityView stopAnimating]; _activityView.hidesWhenStopped = YES; } return _activityView; } - (UILabel*)textLabel { if (!_textLabel) { _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.activityView.frame)+5 , CGRectGetWidth(self.bounds), 20)]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.textColor = [UIColor colorWithHexString:TextGray]; _textLabel.autoresizingMask = AutoresizingAuto; _textLabel.backgroundColor = [UIColor clearColor]; } _textLabel.font = appFont(11, NO); return _textLabel; } - (void)setState:(collectLoadState )sta { if (_state == sta) return; self.activityView.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-25/2, 10, 25, 25); _state = sta; if (sta == RefreshStateNormal) { [self.activityView stopAnimating]; self.textLabel.hidden = NO; self.textLabel.frame = self.bounds; self.textLabel.text = @"上拉加载数据"; }else if (sta == RefreshStateRefreshing) { self.textLabel.hidden = NO; [self.activityView startAnimating]; self.textLabel.frame = CGRectMake(0,CGRectGetMaxY(self.activityView.frame)+5 , CGRectGetWidth(self.bounds), 20); self.textLabel.text = @"正在加载"; if ([delegate conformsToProtocol:@protocol(collectLoadDelegate)] && [delegate respondsToSelector:@selector(startRefresh)]) { [delegate startLoad]; } }else if (sta == RefreshStateCancel) { self.state = RefreshStateNormal; }else if (sta == RefreshStateSucess) { self.state = RefreshStateNormal; }else if (sta == RefreshStateFailed) { // ShowTextMessage(@"加载失败"); self.state = RefreshStateNormal; }else if (sta == RefreshStateNoMore) { [self.activityView stopAnimating]; self.textLabel.hidden = NO; self.textLabel.frame = self.bounds; self.textLabel.text = @"没有更多数据"; }else if (sta == RefreshStatePulling) { [self.activityView stopAnimating]; self.textLabel.hidden = NO; self.textLabel.frame = self.bounds; self.textLabel.text = @"释放加载数据"; } } - (void)layoutSubviews { [super layoutSubviews]; if ([[TPUserDefault instance].isNightMode intValue] > 0) { self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; }else { self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; } } @end @interface TPCollectionController () @end @implementation TPCollectionController @synthesize collectionView; @synthesize collectionLayout = _collectionLayout; @synthesize nextUrl = _nextUrl; @synthesize loadFooter; - (id)initWithCollectionViewLayout:(UICollectionViewFlowLayout *)layout{ if (self = [super init]) { CGFloat width = [UIScreen mainScreen].applicationFrame.size.width; CGFloat height = [UIScreen mainScreen].applicationFrame.size.height; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, width, height)collectionViewLayout:layout]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.autoresizingMask = AutoresizingFull; self.collectionView.backgroundColor = [UIColor clearColor]; layout.footerReferenceSize = CGSizeMake(CGRectGetWidth(self.collectionView.bounds), 60); [self.collectionView registerClass:[collectLoadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:collectLoadIndentfier]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:collectCellIdentifier]; self.collectionView.allowsSelection = YES; self.collectionView.scrollEnabled = YES; self.collectionView.showsHorizontalScrollIndicator= NO; self.collectionView.showsVerticalScrollIndicator= YES; self.collectionView.delaysContentTouches = NO; isCanLoad = NO; isCanRefresh = NO; } return self; } - (id)init { if (self = [super init]) { CGFloat width = [UIScreen mainScreen].applicationFrame.size.width; CGFloat height = [UIScreen mainScreen].applicationFrame.size.height; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.autoresizingMask = AutoresizingFull; self.collectionView.backgroundColor = [UIColor clearColor]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:collectCellIdentifier]; self.collectionView.allowsSelection = YES; self.collectionView.scrollEnabled = YES; self.collectionView.showsHorizontalScrollIndicator= NO; self.collectionView.showsVerticalScrollIndicator= YES; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.collectionView.frame = self.view.bounds; self.collectionView.clipsToBounds = NO; [self.view addSubview:self.collectionView]; // Do any additional setup after loading the view. } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; if (isCanRefresh) { self.collectionView.refreshControl.frame = CGRectMake(0, self.collectionView.refreshControl.frame.origin.y, CGRectGetWidth(self.collectionView.frame), self.collectionView.refreshControl.frame.size.height); } } - (void)configCanRefresh:(BOOL)canRefresh canLoad:(BOOL)canLoad { if (canRefresh || canLoad) { if (canRefresh) { isCanRefresh = YES; NSDictionary *dic = @{@"pattern":@"%d.png",@"drawingStart":@1,@"drawingEnd":@30,@"loadingStart":@31,@"loadingEnd":@73}; self.drawingImgs = [NSMutableArray array]; self.loadingImgs = [NSMutableArray array]; NSUInteger drawingStart = [dic[@"drawingStart"] intValue]; NSUInteger drawingEnd = [dic[@"drawingEnd"] intValue]; NSUInteger laodingStart = [dic[@"loadingStart"] intValue]; NSUInteger loadingEnd = [dic[@"loadingEnd"] intValue]; for (NSUInteger i = drawingStart; i <= drawingEnd; i++) { NSString *fileName = [NSString stringWithFormat:dic[@"pattern"],i]; [self.drawingImgs addObject:Image([NSString stringWithFormat:@"PaperPullImage/%@",fileName])]; } for (NSUInteger i = laodingStart; i <= loadingEnd; i++) { NSString *fileName = [NSString stringWithFormat:dic[@"pattern"],i]; [self.loadingImgs addObject:Image([NSString stringWithFormat:@"PaperPullImage/%@",fileName])]; } __weak typeof(self) Self = self; [self.collectionView addPullToRefreshWithDrawingImgs:self.drawingImgs andLoadingImgs:self.loadingImgs andActionHandler:^{ [Self startRefresh]; }]; // [self.collectionView addHeaderWithTarget:self action:@selector(startRefresh)]; // self.collectionView.headerPullToRefreshText = @"下拉刷新数据"; // self.collectionView.headerReleaseToRefreshText = @"释放开始刷新"; // self.collectionView.headerRefreshingText = @"正在刷新..."; } if (canLoad) { isCanLoad = YES; [self addObserver:self forKeyPath:@"self.collectionView.contentOffset" options:NSKeyValueObservingOptionNew context:nil]; // [self.collectionView addFooterWithTarget:self action:@selector(startLoad)]; // self.collectionView.footerPullToRefreshText = @"上拉加载数据"; // self.collectionView.footerReleaseToRefreshText = @"释放开始加载"; // self.collectionView.footerRefreshingText = @"加载中..."; }else { isCanLoad = NO; } } } - (void)setNextUrl:(NSString *)url { if ([_nextUrl isEqualToString:url]) { if (isBlankString(url)) { [self noMoreLoad]; } return; } _nextUrl = url; if (isBlankString(url)) { [self noMoreLoad]; }else { self.loadFooter.state = RefreshStateNormal; } } - (void)setCollectionLayout:(UICollectionViewFlowLayout *)layout { _collectionLayout = layout; layout.footerReferenceSize = CGSizeMake(CGRectGetWidth(self.collectionView.bounds), 60); [self.collectionView setCollectionViewLayout:layout animated:YES]; } #pragma mark <UICollectionViewDataSource> - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collection cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:collectCellIdentifier forIndexPath:indexPath]; // cell.backgroundColor = RandomColor; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(300, 335/2); } -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { UIEdgeInsets top = {0,10,0,10}; return top; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collection viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { collectLoadView *reusableview = nil; if (kind == UICollectionElementKindSectionFooter && isCanLoad){ collectLoadView *footerview = [collection dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:collectLoadIndentfier forIndexPath:indexPath]; if (!self.loadFooter) { self.loadFooter = footerview; self.loadFooter.hidden = YES; } reusableview = footerview; self.loadFooter.delegate = self; } return reusableview; } #pragma mark <UICollectionViewDelegate> /* // Uncomment this method to specify if the specified item should be highlighted during tracking - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } */ /* // Uncomment this method to specify if the specified item should be selected - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } */ /* // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath { return NO; } - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { return NO; } - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { } */ #pragma mark - refresh and load method - (void)manualRefresh { // [self.collectionView headerBeginRefreshing]; [self.collectionView.refreshControl manulHeadRefresh]; } - (void)manualLoad { [self startLoad]; } - (void)startRefresh { [self pullRefreshHandler]; [self loadSuccess]; } - (void)startLoad { if (isBlankString(self.nextUrl)) { self.loadFooter.state = RefreshStateNoMore; return; } [self pullLoadMoreHander]; } - (void)pullRefreshHandler{ } - (void)pullLoadMoreHander { } - (void)endRefresh:(void (^)(void))animate completion:(void (^)(void))completion { [self.collectionView didFinishPullToRefresh:^{ if (animate) { animate(); } } completion:^{ if (completion) { completion(); } }]; } - (void)noMoreLoad { self.loadFooter.state = RefreshStateNoMore; } - (void)loadSuccess { self.loadFooter.state = RefreshStateSucess; } - (void)loadFailed { self.loadFooter.state = RefreshStateFailed; } - (void)cancelLoadMore { self.loadFooter.state = RefreshStateNormal; } #pragma mark - kvo #pragma mark - KVO call back - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!isCanLoad) { return; } if (!self.collectionView.userInteractionEnabled || self.collectionView.alpha <= 0.01 || self.collectionView.hidden) return; if (isCanLoad) { [self loadMethod]; } } - (void)loadMethod { if (self.collectionView.contentOffset.y < 0) { //顶部操作直接返回 return; } if (self.loadFooter.state == RefreshStateRefreshing || self.loadFooter.state == RefreshStateNoMore) { self.loadFooter.hidden = NO; return; } CGPoint offset = self.collectionView.contentOffset; CGRect bounds = self.collectionView.bounds; CGSize size = self.collectionView.contentSize; UIEdgeInsets inset = self.collectionView.contentInset; CGFloat currentOffset = offset.y + bounds.size.height - inset.bottom; CGFloat maximumOffset = size.height; CGFloat compareOffset = currentOffset - maximumOffset; if (self.collectionView.isDragging ) { if (compareOffset<15 ) { if (!self.collectionView.isDecelerating) { self.loadFooter.state = RefreshStateNormal; } }else if (self.loadFooter.state == RefreshStateNormal && (currentOffset - maximumOffset)>20) { self.loadFooter.hidden = NO; if (!self.collectionView.isDecelerating) { self.loadFooter.state = RefreshStatePulling; //转为即将刷新 }else { self.loadFooter.state = RefreshStateRefreshing; } } }else { self.loadFooter.hidden = NO; if (self.loadFooter.state == RefreshStatePulling) { self.loadFooter.state = RefreshStateRefreshing; }else { self.loadFooter.state = RefreshStateNormal; } } } #pragma mark - remote method - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{ ShowTextMessage(message); [self endRefresh:nil completion:nil]; [self loadSuccess]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { if (isCanLoad) { [self removeObserver:self forKeyPath:@"self.collectionView.contentOffset"]; } [self.collectionView removePullToRefresh]; [self.collectionView.refreshControl removeFromSuperview]; self.collectionView.refreshControl = nil; [self.collectionView removeFromSuperview]; self.collectionView = nil; [self.drawingImgs removeAllObjects]; [self.loadingImgs removeAllObjects]; } @end