码迷,mamicode.com
首页 > 其他好文 > 详细

[翻译] GMCPagingScrollView

时间:2014-05-26 11:19:10      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

GMCPagingScrollView

bubuko.com,布布扣

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中提供的源码:

bubuko.com,布布扣
#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
bubuko.com,布布扣

效果:

bubuko.com,布布扣

 

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

bubuko.com,布布扣
#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
bubuko.com,布布扣

bubuko.com,布布扣

 

 

[翻译] GMCPagingScrollView,布布扣,bubuko.com

[翻译] GMCPagingScrollView

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/YouXianMing/p/3746970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!