首页 > 代码库 > [翻译] GMCPagingScrollView

[翻译] GMCPagingScrollView

GMCPagingScrollView

https://github.com/GalacticMegacorp/GMCPagingScrollView

 

GMCPagingScrollView is a UIView containing a horizontally scrolling paging UIScrollView that supports page preloading, page dequeueing, and infinite scrolling.

GMCPagingScrollView是一个UIView,包含了一个水平方向翻页滚动的UIScrollView,支持预加载,重用以及无线滚动.

 

demo中提供的源码:

#import "DemoViewController.h"
#import "GMCPagingScrollView.h"

static NSString * const kPageIdentifier = @"Page";

@interface DemoViewController () <GMCPagingScrollViewDataSource, GMCPagingScrollViewDelegate>

@property (nonatomic, strong) GMCPagingScrollView *pagingScrollView;

@end

@implementation DemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pagingScrollView = [[GMCPagingScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    self.pagingScrollView.center = self.view.center;
    self.pagingScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.pagingScrollView.dataSource = self;
    self.pagingScrollView.delegate   = self;
    self.pagingScrollView.infiniteScroll = YES;
    self.pagingScrollView.interpageSpacing = 0;
    [self.view addSubview:self.pagingScrollView];
    
    [self.pagingScrollView registerClass:[UIView class] forReuseIdentifier:kPageIdentifier];
    
    [self.pagingScrollView reloadData];
}

#pragma mark - GMCPagingScrollViewDataSource

- (NSUInteger)numberOfPagesInPagingScrollView:(GMCPagingScrollView *)pagingScrollView {
    return 3;
}

- (UIView *)pagingScrollView:(GMCPagingScrollView *)pagingScrollView pageForIndex:(NSUInteger)index {
    UIView *page = [pagingScrollView dequeueReusablePageWithIdentifier:kPageIdentifier];
    
    switch (index) {
        case 0:
            page.backgroundColor = [UIColor redColor];
            break;
        case 1:
            page.backgroundColor = [UIColor greenColor];
            break;
        case 2:
            page.backgroundColor = [UIColor blueColor];
            break;
    }
    
    return page;
}

- (void)pagingScrollViewDidScroll:(GMCPagingScrollView *)pagingScrollView
{
    NSLog(@"x = %f", pagingScrollView.scrollView.contentOffset.x);
}

@end

效果:

 

修改一下并使用SDWebImage来测试:

#import "DemoViewController.h"
#import "GMCPagingScrollView.h"
#import "SDWebImage.h"

static NSString * const kPageIdentifier = @"Page";

@interface DemoViewController () <GMCPagingScrollViewDataSource, GMCPagingScrollViewDelegate>

@property (nonatomic, strong) GMCPagingScrollView *pagingScrollView;
@property (nonatomic, strong) NSArray             *dataArray;

@end

@implementation DemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    _dataArray =     @[@"http://img4.duitang.com/uploads/item/201307/29/20130729153409_YCfU2.thumb.200_0.jpeg",
      @"http://cdn.duitang.com/uploads/item/201212/08/20121208141407_3YGMi.thumb.200_0.jpeg",
      @"http://cdn.duitang.com/uploads/item/201308/26/20130826211332_WZ4is.thumb.200_0.jpeg",
      @"http://img4.duitang.com/uploads/item/201301/21/20130121223918_aFk4h.thumb.200_0.jpeg",
      @"http://img4.duitang.com/uploads/item/201309/15/20130915114846_nsy2A.thumb.200_0.jpeg",
      @"http://cdn.duitang.com/uploads/item/201306/20/20130620142009_X3fv3.thumb.200_0.jpeg",
      @"http://img4.duitang.com/uploads/item/201306/17/20130617202501_Z2ZNP.thumb.200_0.jpeg",
      @"http://img4.duitang.com/uploads/item/201201/23/20120123181139_EvHrc.thumb.200_0.jpg",
      @"http://img4.duitang.com/uploads/item/201108/24/20110824232929_T85Zt.thumb.200_0.jpg",
      @"http://cdn.duitang.com/uploads/blog/201308/06/20130806213223_Q2Jfj.thumb.200_0.jpeg",
      @"http://cdn.duitang.com/uploads/item/201311/10/20131110141543_UMV24.thumb.200_0.jpeg",
      @"http://cdn.duitang.com/uploads/item/201307/18/20130718225516_RBMnr.thumb.200_0.jpeg",
      @"http://img4.duitang.com/uploads/item/201202/05/20120205163116_x2F4E.thumb.200_0.jpg",
      @"http://img4.duitang.com/uploads/item/201202/19/20120219150016_r48NA.thumb.200_0.jpg",];
    
    self.pagingScrollView = [[GMCPagingScrollView alloc] initWithFrame:self.view.bounds];
    self.pagingScrollView.dataSource = self;
    self.pagingScrollView.delegate   = self;
    self.pagingScrollView.infiniteScroll = YES;
    self.pagingScrollView.interpageSpacing = 0;
    [self.view addSubview:self.pagingScrollView];
    
    [self.pagingScrollView registerClass:[UIImageView class]
                      forReuseIdentifier:kPageIdentifier];
    
    [self.pagingScrollView reloadData];
}

#pragma mark - GMCPagingScrollViewDataSource

- (NSUInteger)numberOfPagesInPagingScrollView:(GMCPagingScrollView *)pagingScrollView {
    return [_dataArray count];
}

- (UIView *)pagingScrollView:(GMCPagingScrollView *)pagingScrollView pageForIndex:(NSUInteger)index {
    UIImageView *page = [pagingScrollView dequeueReusablePageWithIdentifier:kPageIdentifier];
    
    [page setImageWithURL:[NSURL URLWithString:_dataArray[index]]];
    page.contentMode = UIViewContentModeScaleAspectFit;
    page.layer.masksToBounds = YES;
    
    return page;
}

- (void)pagingScrollViewDidScroll:(GMCPagingScrollView *)pagingScrollView
{
    NSLog(@"x = %f", pagingScrollView.scrollView.contentOffset.x);
}

@end