iOS scrollview实现三屏复用循环广告
循环广告我们在开发中已经是熟得不能再熟了,今天整理这篇scrollview三屏复用广告。
原理使用scrollview里的三个imageview分别去加载不同的图片,用少量的资源来显示大量或不确定的广告数量,不然如果用普通方法实现广告,难道10个广告用12个scrollview的contentsize去做,岂不是太浪费资源了
代码如下,实现所有数量的循环广告,当广告只有一个时,仅采用单图显示,>=2个广告时,自动采用三屏复用
这里添加图片的方式是通过网络请求,更新服务器上的广告,如果仅使用本地广告,可以将.m文件里的全部图片的添加方式
如:
self.endimageview.image = self.imagearray[endimagecount];
修改为
self.endimageview.image = [uiimage imagenamed:self.imagearray[endimagecount]];
然后在使用该类时,直接将本地图片的名字用数组传过去就行了,如
cview.imagearray = [[nsmutablearray alloc]initwithobjects:@"图片1",@"图片2",@"图片3", nil];
或者不改则使用方法如
nsarray *imagearr = [[nsarray alloc]initwithobjects:@"banner_理财.jpg",@"banner_惠普",@"banner_炒股", nil]; for (int i=0; i<3; i++) { uiimage *cirimage1 = [uiimage imagenamed:imagearr[i]]; [cirscrollview.imagearray addobject:cirimage1]; }
如果图片给的是地址那可以用imagewithurl这个方法来获取图片。
下面讲从服务器获取的广告方式,请求服务器图片及解析这里就不讲了,仅从获取到的data数据后开始。
先新建一个类继承uiview。
.h文件
#import <uikit/uikit.h> @interface circulatescrollview : uiview @property (nonatomic,strong)nsmutablearray *imagearray;//图片数组 @property (nonatomic,strong)uiscrollview *circulatescrollview;//广告 /* 三屏复用广告 适用范围:网络请求或固定本地的广告图片 适用所有数量广告,广告>=2时自动采用三屏复用技术 使用方法:例 在需要添加广告的控制器里面 circulatescrollview *cview = [[circulatescrollview alloc]initwithframe:cgrectmake(0, 20, 320, 200)]; for (int i=0; i<3; i++) { uiimage *image = [uiimage imagenamed:@"旅行图1"];//传进图片名字方式 //uiimage *image = uiimage imagewithdata:data];//传进data数据图片方式将服务器请求到的data数据图片转换成image形式再传输 [cview.imagearray addobject:image]; } [self.view addsubview:cview]; */ /* 图片转换nsdata方法 测试可用 nsdata * data = uiimagejpegrepresentation(image, 1); */ @end
.m文件
实现方法是这三个成员变量,用来读取传输过来的图片在数组中的位置,三屏复用里,我们显示的位置是scrollview的中间位置,左边广告是全部广告的最后一个,中间显示第一个,右边的显示第二个,然后根据左滑成员变量递增,当变量递增到超过广告总数时,重新赋值第一个广告,而右滑递减,递减至-1时,即不在数组范围时,重新赋值广告数组的最后一个
#import "circulatescrollview.h"
#define viewwidth self.frame.size.width #define viewheight self.frame.size.height #define allimagecount self.imagearray.count-1 @interface circulatescrollview()<uiscrollviewdelegate> { nsinteger endimagecount;//左边图片 nsinteger oneimagecount;//中间图片[当前看到的图片] nsinteger secondimagecount;//右边图片 } @property (nonatomic,strong)uiimageview *endimageview; @property (nonatomic,strong)uiimageview *oneimageview; @property (nonatomic,strong)uiimageview *secondimageview; @property (nonatomic,strong)uipagecontrol *pagectl; @end @implementation circulatescrollview -(id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { } return self; } -(nsmutablearray *)imagearray { if (!_imagearray) { _imagearray = [[nsmutablearray alloc]init]; } return _imagearray; } - (void)drawrect:(cgrect)rect { self.circulatescrollview = [[uiscrollview alloc]initwithframe:cgrectmake(0, 0, viewwidth, viewheight)]; endimagecount = self.imagearray.count-1; oneimagecount = 0; secondimagecount = 1; self.circulatescrollview.showshorizontalscrollindicator = no; self.circulatescrollview.pagingenabled = yes; self.circulatescrollview.delegate = self; self.circulatescrollview.bounces = no; self.circulatescrollview.contentoffset = cgpointmake(viewwidth, 0); self.backgroundcolor = [uicolor whitecolor]; if (!self.imagearray.count) { nslog(@"图片数组为空"); return; } //若广告数量少于2张则不采用三屏复用技术 if (self.imagearray.count<=1){ self.circulatescrollview.contentsize = cgsizemake(viewwidth, viewheight); self.endimageview = [[uiimageview alloc]initwithframe:cgrectmake(0, 0, viewwidth, viewheight)]; self.endimageview.image = self.imagearray[endimagecount]; [self.circulatescrollview addsubview:self.endimageview]; [self addsubview:self.circulatescrollview]; }else{ self.circulatescrollview.contentsize = cgsizemake(viewwidth*3, viewheight); //左 self.endimageview = [[uiimageview alloc]initwithframe:cgrectmake(0, 0, viewwidth, viewheight)]; self.endimageview.image = self.imagearray[endimagecount]; [self.circulatescrollview addsubview:self.endimageview]; //中 self.oneimageview = [[uiimageview alloc]initwithframe:cgrectmake(viewwidth, 0, viewwidth, viewheight)]; self.oneimageview.image = self.imagearray[oneimagecount]; [self.circulatescrollview addsubview:self.oneimageview]; //右 self.secondimageview = [[uiimageview alloc]initwithframe:cgrectmake(viewwidth*2, 0, viewwidth, viewheight)]; self.secondimageview.image = self.imagearray[secondimagecount]; [self.circulatescrollview addsubview:self.secondimageview]; [self addsubview:self.circulatescrollview]; [self pagenumcontrol]; } } //添加页符 -(void)pagenumcontrol { self.pagectl = [[uipagecontrol alloc]initwithframe:cgrectmake(0, viewheight-20, viewwidth, 20)]; self.pagectl.backgroundcolor = [uicolor lightgraycolor]; self.pagectl.currentpageindicatortintcolor = [uicolor greencolor]; self.pagectl.pageindicatortintcolor = [uicolor whitecolor]; self.pagectl.alpha = 0.7; self.pagectl.numberofpages = allimagecount+1; [self addsubview:self.pagectl]; } -(void)scrollviewdidenddecelerating:(uiscrollview *)scrollview { if (scrollview.contentoffset.x == 0) { endimagecount--; oneimagecount--; secondimagecount--; if (endimagecount<0) { endimagecount = allimagecount; }else if (oneimagecount<0){ oneimagecount = allimagecount; } //适配2张图片 if (secondimagecount<0){ secondimagecount = allimagecount; } //nslog(@"endimagecount=%ld oneimagecount=%ld secondimagecount=%ld",endimagecount,oneimagecount,secondimagecount); }else if(scrollview.contentoffset.x == viewwidth*2){ endimagecount++; oneimagecount++; secondimagecount++; if (endimagecount>allimagecount) { endimagecount = 0; }else if (oneimagecount>allimagecount){ oneimagecount = 0; } //适配2张图片 if (secondimagecount>allimagecount){ secondimagecount = 0; } } //重新加载显示当前位置的图片 scrollview.contentoffset = cgpointmake(viewwidth, 0); self.endimageview.image = self.imagearray[endimagecount]; self.oneimageview.image = self.imagearray[oneimagecount]; self.secondimageview.image = self.imagearray[secondimagecount]; self.pagectl.currentpage = oneimagecount; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: iOS自学笔记之XIB的使用教程
下一篇: iOS实现带动画的环形进度条