iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView

来源:互联网 发布:photo picture软件 编辑:程序博客网 时间:2024/05/20 06:30

下载链接:github不断更新地址:https://github.com/gsdios/SDCycleScrollView

使用原理:采用UICollectionView的重用机制和循环滚动的方式实现图片的无限轮播,播放非常顺畅,解决了UISCrollView使用时从最后一张跳到第一张时的生硬状态。

主要类截图:

SDCollectionViewCell:用来重用的item,即显示图片的视图;

SDCycleScrollView: 对外提供的一个创建轮播器的接口类,使用者就是直接使用这个类来实现图片轮播的;

这几个类主要是用来处理分页节点的,可以使用默认的原点分页节点,也可以使用图片节点;

TAPageControl:顾名思义,可知这个是用来设置分页的;

一个简化使用视图frame结构体及其结构体中属性的视图分类。 

 

无限循环自动图片轮播器(一步设置即可使用)
 
// 网络加载图片的轮播器
SDCycleScrollView *cycleScrollView = [cycleScrollViewWithFrame:frame delegate:delegate placeholderImage:placeholderImage];
cycleScrollView.imageURLStringsGroup = imagesURLStrings;

// 本地加载图片的轮播器
SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:frame  imageNamesGroup:图片数组];
 
 
详情页面地址:http://code.cocoachina.com/view/129190
更改记录:
2016.01.15 -- 兼容assets存放的本地图片
2016.01.06 -- 0.图片管理使用SDWebImage;1.优化内存,提升性能;2.添加图片contentmode接口;3.block监听点击接口;4.滚动到某张图片监听;5.增加自定义图片pageControl接口;6.其他等等。其中有一处接口改动:pagecontrol的小圆点自定义接口改为:currentPageDotColor、pageDotColor、currentPageDotImage、pageDotImage。

PS:
现已支持cocoapods导入:pod 'SDCycleScrollView','~> 1.6' 
 
SDCycleScrollView.h文件中的代码如下:
 View Code
 
具体代码演示如下:
1.倒入头文件和设置属性
复制代码
#import <SDCycleScrollView.h> // 我采用的是CopcoaPods管理工具导入的第三方库,所以使用<>导入头文件名@interface ViewController ()<SDCycleScrollViewDelegate>@property (strong,nonatomic)NSArray *localImages;//本地图片@property (strong,nonatomic)NSArray *netImages;  //网络图片@property (strong,nonatomic)SDCycleScrollView *cycleScrollView;//轮播器@end
复制代码

2.懒加载本地图片和网络图片

复制代码
/** *  懒加载本地图片数据 */-(NSArray *)localImages{        if (!_localImages) {        _localImages = @[@"1.png",@"2.png",@"3.png",@"4.png"];    }    return _localImages;}/** *  懒加载网络图片数据 */-(NSArray *)netImages{        if (!_netImages) {        _netImages = @[                       @"http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40f507b2e99aa64034f78f01930.jpg",                       @"http://b.hiphotos.baidu.com/zhidao/pic/item/4b90f603738da9770889666fb151f8198718e3d4.jpg",                       @"http://g.hiphotos.baidu.com/zhidao/pic/item/f2deb48f8c5494ee4e84ef5d2cf5e0fe98257ed4.jpg",                       @"http://d.hiphotos.baidu.com/zhidao/pic/item/9922720e0cf3d7ca104edf32f31fbe096b63a93e.jpg"                     ];    }    return _netImages;}
复制代码

3-1.封装方法,轮播本地图片

复制代码
/** *  轮播本地图片 */-(void)ScrollLocalImages{        /** 测试本地图片数据*/    CGRect rect = CGRectMake(0,150, self.view.bounds.size.width, 400);    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:rect imageNamesGroup:self.localImages];        //设置图片视图显示类型    self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;        //设置轮播视图的分页控件的显示    self.cycleScrollView.showPageControl = YES;        //设置轮播视图分也控件的位置    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;        //当前分页控件小圆标颜色    self.cycleScrollView.currentPageDotColor = [UIColor redColor];        //其他分页控件小圆标颜色    self.cycleScrollView.pageDotColor = [UIColor purpleColor];        [self.view addSubview:self.cycleScrollView];}
复制代码

3-2.封装方法,轮播网络图片

复制代码
/** *  轮播网络图片 */-(void)ScrollNetWorkImages{        /** 测试本地图片数据*/    CGRect rect = CGRectMake(0,150, self.view.bounds.size.width, 400);    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:rect delegate:self placeholderImage:[UIImage imageNamed:@"PlacehoderImage.png"]];        //设置网络图片数组    self.cycleScrollView.imageURLStringsGroup = self.netImages;        //设置图片视图显示类型    self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;        //设置轮播视图的分页控件的显示    self.cycleScrollView.showPageControl = YES;        //设置轮播视图分也控件的位置    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;        //当前分页控件小圆标图片    self.cycleScrollView.pageDotImage = [UIImage imageNamed:@"pageCon.png"];    //其他分页控件小圆标图片    self.cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"pageConSel.png"];            [self.view addSubview:self.cycleScrollView];}#pragma mark - 代理方法/** 点击图片回调 */- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{        //NSLog(@"%ld",index);}/** 图片滚动回调 */- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{       //NSLog(@"%ld",index);}
复制代码

 

测试1:
- (void)viewDidLoad {    [super viewDidLoad];        [self ScrollLocalImages];}
测试2: (一开始有占位图片,因为已经测试过,做了缓存,所以没有贴出截图)
- (void)viewDidLoad {    [super viewDidLoad];        [self ScrollNetWorkImages];}

 
0 0