| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- //
- // GKBaseListViewController.m
- // GKPageScrollViewDemo
- //
- // Created by QuintGao on 2018/12/11.
- // Copyright © 2018 QuintGao. All rights reserved.
- //
- #import "GKBaseListViewController.h"
- #import <MJRefresh/MJRefresh.h>
- #import <WebKit/WebKit.h>
- @interface GKBaseCollectionViewLayout : UICollectionViewFlowLayout
- @end
- @implementation GKBaseCollectionViewLayout
- - (CGSize)collectionViewContentSize {
- CGFloat minContentSizeHeight = self.collectionView.bounds.size.height;
- CGSize size = [super collectionViewContentSize];
- if (size.height < minContentSizeHeight) {
- return CGSizeMake(size.width, minContentSizeHeight);
- }
- return size;
- }
- @end
- @interface GKBaseListViewController()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, WKNavigationDelegate>
- @property (nonatomic, strong) UIImageView *loadingView;
- @property (nonatomic, strong) UILabel *loadLabel;
- @property (nonatomic, copy) void(^listScrollViewScrollCallback)(UIScrollView *scrollView);
- @property (nonatomic, assign) GKBaseListType listType;
- @property (nonatomic, weak) UIScrollView *currentScrollView;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) UIScrollView *scrollView;
- @property (nonatomic, strong) WKWebView *webView;
- @property (nonatomic, copy) void(^refreshCompletion)(void);
- @end
- @implementation GKBaseListViewController
- - (instancetype)initWithListType:(GKBaseListType)listType {
- if (self = [super init]) {
- self.listType = listType;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- if (self.listType == GKBaseListType_UITableView) {
- [self.view addSubview:self.tableView];
- self.currentScrollView = self.tableView;
- }else if (self.listType == GKBaseListType_UICollectionView) {
- [self.view addSubview:self.collectionView];
- self.currentScrollView = self.collectionView;
- }else if (self.listType == GKBaseListType_UIScrollView) {
- [self.view addSubview:self.scrollView];
- self.currentScrollView = self.scrollView;
- }else if (self.listType == GKBaseListType_WKWebView) {
- [self.view addSubview:self.webView];
- self.currentScrollView = self.webView.scrollView;
- }
-
- if (self.listType == GKBaseListType_WKWebView) {
- [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.view);
- }];
- }else {
- [self.currentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.view);
- }];
- }
-
- if (self.listType != GKBaseListType_WKWebView && !self.disableLoadMore) {
- __weak __typeof(self) weakSelf = self;
- self.currentScrollView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
- __strong __typeof(weakSelf) self = weakSelf;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self loadMoreData];
- });
- }];
- }
-
- if (self.shouldLoadData) {
- [self.currentScrollView addSubview:self.loadingView];
- [self.currentScrollView addSubview:self.loadLabel];
- [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.currentScrollView).offset(40.0f);
- make.centerX.equalTo(self.currentScrollView);
- }];
-
- [self.loadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.loadingView.mas_bottom).offset(10.0f);
- make.centerX.equalTo(self.loadingView);
- }];
-
- self.count = 0;
- self.currentScrollView.mj_footer.hidden = self.count == 0;
- [self reloadData];
-
- [self showLoading];
- if (self.listType == GKBaseListType_WKWebView) {
- [self loadData];
- }else {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self hideLoading];
- [self loadData];
- });
- }
- }else {
- [self loadData];
- }
- }
- //- (void)viewWillAppear:(BOOL)animated {
- // [super viewWillAppear:animated];
- //
- // NSLog(@"%zd--%s", self.index, __func__);
- //}
- //
- //- (void)viewDidAppear:(BOOL)animated {
- // [super viewDidAppear:animated];
- //
- // NSLog(@"%zd--%s", self.index, __func__);
- //}
- //
- //- (void)viewWillDisappear:(BOOL)animated {
- // [super viewWillDisappear:animated];
- //
- // NSLog(@"%zd--%s", self.index, __func__);
- //}
- //
- //- (void)viewDidDisappear:(BOOL)animated {
- // [super viewDidDisappear:animated];
- //
- // NSLog(@"%zd--%s", self.index, __func__);
- //}
- - (void)viewWillLayoutSubviews {
- [super viewWillLayoutSubviews];
-
- if (!self.view.superview) return;
- if (CGSizeEqualToSize(self.view.superview.frame.size, CGSizeZero)) return;
-
-
- GKBaseCollectionViewLayout *layout = (GKBaseCollectionViewLayout *)self.collectionView.collectionViewLayout;
- CGFloat minW = MIN(self.view.superview.frame.size.width, self.view.superview.frame.size.width);
-
- layout.itemSize = CGSizeMake((minW - 60) / 2, (minW - 60) / 2);
-
- CGRect frame = self.view.frame;
- frame.size = self.view.superview.bounds.size;
- self.view.frame = frame;
- }
- - (void)loadData {
- if (self.count == 0) {
- self.count = 30;
- }
-
- if (self.listType == GKBaseListType_UIScrollView) {
- [self addCellToScrollView];
- }else if (self.listType == GKBaseListType_WKWebView) {
- [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://github.com/QuintGao/GKPageScrollView"]]];
- }
-
- [self reloadData];
- }
- - (void)loadMoreData {
- self.count += 20;
-
- if (self.count >= 100) {
- [self.currentScrollView.mj_footer endRefreshingWithNoMoreData];
- }else {
- [self.currentScrollView.mj_footer endRefreshing];
- }
-
- if (self.listType == GKBaseListType_UIScrollView) {
- [self addCellToScrollView];
- }
-
- [self reloadData];
- }
- - (void)addCellToScrollView {
- [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if ([obj isKindOfClass:UILabel.class]) {
- [obj removeFromSuperview];
- }
- }];
-
- UIView *lastView = nil;
- for (NSInteger i = 0; i < self.count; i++) {
- UILabel *label = [UILabel new];
- label.textColor = [UIColor blackColor];
- label.font = [UIFont systemFontOfSize:16.0f];
- label.text = [NSString stringWithFormat:@"第%zd行", i + 1];
- [self.scrollView addSubview:label];
-
- [label mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(@30);
- make.top.equalTo(lastView ? lastView.mas_bottom : @0);
- make.width.mas_equalTo(self.scrollView.mas_width);
- make.height.mas_equalTo(50.0f);
- }];
-
- lastView = label;
- }
-
- [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self.view);
- make.bottom.equalTo(lastView.mas_bottom);
- }];
- }
- - (void)showLoading {
- self.loadingView.hidden = NO;
- self.loadLabel.hidden = NO;
- [self.loadingView startAnimating];
- }
- - (void)hideLoading {
- [self.loadingView stopAnimating];
- self.loadingView.hidden = YES;
- self.loadLabel.hidden = YES;
- }
- - (void)addHeaderRefresh {
- __weak __typeof(self) weakSelf = self;
- self.currentScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
- __strong __typeof(weakSelf) self = weakSelf;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.count = 30;
- [self reloadData];
-
- [self.currentScrollView.mj_header endRefreshing];
-
- !self.refreshCompletion ?: self.refreshCompletion();
- });
- }];
- }
- - (void)reloadData {
- if (self.listType == GKBaseListType_UITableView) {
- [self.tableView reloadData];
- }else if (self.listType == GKBaseListType_UICollectionView) {
- [self.collectionView reloadData];
- }
- }
- - (void)refreshWithCompletion:(nullable void(^)(void))completion {
-
- self.refreshCompletion = completion;
- [self.currentScrollView.mj_header beginRefreshing];
- }
- #pragma mark - UITableViewDataSource & UITableViewDelegate
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- self.tableView.mj_footer.hidden = self.count == 0;
- return self.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];
- cell.textLabel.text = [NSString stringWithFormat:@"第%zd行", indexPath.row + 1];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- !self.listItemClick ?: self.listItemClick(self, indexPath);
- }
- #pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- self.collectionView.mj_footer.hidden = self.count == 0;
- return self.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
- cell.contentView.backgroundColor = [UIColor redColor];
- [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
-
- UILabel *textLabel = [UILabel new];
- textLabel.font = [UIFont systemFontOfSize:16.0f];
- textLabel.textColor = [UIColor blackColor];
- textLabel.text = [NSString stringWithFormat:@"第%zd", indexPath.item + 1];
- [cell.contentView addSubview:textLabel];
- [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(cell.contentView);
- }];
-
- return cell;
- }
- #pragma mark - WKNavigationDelegate
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
- NSLog(@"加载成功");
- [self hideLoading];
- }
- - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
- NSLog(@"加载失败");
- [self hideLoading];
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- !self.listScrollViewScrollCallback ? : self.listScrollViewScrollCallback(scrollView);
- }
- #pragma mark - GKPageListViewDelegate or GKPageSmoothListViewDelegate
- - (UIView *)listView {
- return self.view;
- }
- - (UIScrollView *)listScrollView {
- return self.currentScrollView;
- }
- - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
- self.listScrollViewScrollCallback = callback;
- }
- - (UIImage *)changeImageWithImage:(UIImage *)image color:(UIColor *)color {
- UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(context, 0, image.size.height);
- CGContextScaleCTM(context, 1.0, -1.0);
- CGContextSetBlendMode(context, kCGBlendModeNormal);
- CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
- CGContextClipToMask(context, rect, image.CGImage);
- [color setFill];
- CGContextFillRect(context, rect);
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- - (void)listViewWillAppear {
- NSLog(@"%zd--%s", self.index, __func__);
- }
- - (void)listViewDidAppear {
- NSLog(@"%zd--%s", self.index, __func__);
- }
- - (void)listViewWillDisappear {
- NSLog(@"%zd--%s", self.index, __func__);
- }
- - (void)listViewDidDisappear {
- NSLog(@"%zd--%s", self.index, __func__);
- }
- #pragma mark - 懒加载
- - (UITableView *)tableView {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- if (@available(iOS 11.0, *)) {
- _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"listCell"];
- }
- return _tableView;
- }
- - (UICollectionView *)collectionView {
- if (!_collectionView) {
- GKBaseCollectionViewLayout *layout = [GKBaseCollectionViewLayout new];
- layout.minimumLineSpacing = 20;
- layout.minimumInteritemSpacing = 20;
-
- CGFloat minW = MIN(self.view.frame.size.width, self.view.frame.size.height);
-
- layout.itemSize = CGSizeMake((minW - 60)/2, (minW - 60)/2);
- layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
-
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- _collectionView.dataSource = self;
- _collectionView.delegate = self;
- [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
- _collectionView.backgroundColor = [UIColor whiteColor];
- if (@available(iOS 11.0, *)) {
- _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- _collectionView.alwaysBounceVertical = YES;
- }
- return _collectionView;
- }
- - (UIScrollView *)scrollView {
- if (!_scrollView) {
- _scrollView = [[UIScrollView alloc] init];
- _scrollView.delegate = self;
- if (@available(iOS 11.0, *)) {
- _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- _scrollView.backgroundColor = [UIColor whiteColor];
- _scrollView.alwaysBounceVertical = YES;
- }
- return _scrollView;
- }
- - (WKWebView *)webView {
- if (!_webView) {
- WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
-
- _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
- _webView.scrollView.delegate = self;
- if (@available(iOS 11.0, *)) {
- _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- _webView.navigationDelegate = self;
- }
- return _webView;
- }
- - (UIImageView *)loadingView {
- if (!_loadingView) {
- NSMutableArray *images = [NSMutableArray new];
- for (NSInteger i = 0; i < 4; i++) {
- NSString *imageName = [NSString stringWithFormat:@"cm2_list_icn_loading%zd", i + 1];
-
- UIImage *img = [self changeImageWithImage:[UIImage imageNamed:imageName] color:RGB(200, 38, 39)];
-
- [images addObject:img];
- }
-
- for (NSInteger i = 4; i > 0; i--) {
- NSString *imageName = [NSString stringWithFormat:@"cm2_list_icn_loading%zd", i];
-
- UIImage *img = [self changeImageWithImage:[UIImage imageNamed:imageName] color:RGB(200, 38, 39)];
-
- [images addObject:img];
- }
-
- UIImageView *loadingView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)];
- loadingView.animationImages = images;
- loadingView.animationDuration = 0.75;
- loadingView.hidden = YES;
-
- _loadingView = loadingView;
- }
- return _loadingView;
- }
- - (UILabel *)loadLabel {
- if (!_loadLabel) {
- _loadLabel = [UILabel new];
- _loadLabel.font = [UIFont systemFontOfSize:14.0f];
- _loadLabel.textColor = [UIColor grayColor];
- _loadLabel.text = @"正在加载...";
- _loadLabel.hidden = YES;
- }
- return _loadLabel;
- }
- @end
|