循环广告我们在开发中已经是熟得不能再熟了,今天整理这篇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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。