首页 > 移动平台 > 详细

iOS实现三屏复用循环广告

时间:2016-02-16 11:16:33      阅读:616      评论:0      收藏:0      [点我收藏+]

循环广告我们在开发中已经是熟得不能再熟了,今天整理这篇scrollview三屏复用广告

原理使用scrollview里的三个imageview分别去加载不同的图片,用少量的资源来显示大量或不确定的广告数量,不然如果用普通方法实现广告,难道10个广告用12个scrollview的contentsize去做,岂不是太浪费资源了

代码如下,实现所有数量的循环广告,当广告只有一个时,仅采用单图显示,>=2个广告时,自动采用三屏复用

先新建一个类继承UIView,

.h里

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface CirculateScrollview : UIView
 4 
 5 @property (nonatomic,strong)NSMutableArray *imageArray;//图片数组
 6 @property (nonatomic,strong)UIScrollView *circulateScrollView;//广告
 7 
 8 /*
 9  三屏复用广告
10  适用范围:网络请求或固定本地的广告图片
11         适用所有数量广告,广告>=2时自动采用三屏复用技术
12  使用方法:例
13  在需要添加广告的控制器里面
14  
15  CirculateScrollview *cview = [[CirculateScrollview alloc]initWithFrame:CGRectMake(0, 20, 320, 200)];
16  for (int i=0; i<3; i++) {
17  UIImage *image = [UIImage imageNamed:@"旅行图1"];//传进图片名字方式
18  //UIImage *image = UIImage imageWithData:data];//传进data数据图片方式
19  [cview.imageArray addObject:image];
20  }
21  [self.view addSubview:cview];
22  
23  */
24 
25 
26 /*
27  图片转换NSData方法
28  测试可用
29  NSData * data = UIImageJPEGRepresentation(image, 1);
30  */
31 
32 @end

.m文件里

实现方法是这三个成员变量,用来读取传输过来的图片在数组中的位置,三屏复用里,我们显示的位置是scrollview的中间位置,左边广告是全部广告的最后一个,中间显示第一个,右边的显示第二个,然后根据左滑成员变量递增,当变量递增到超过广告总数时,重新赋值第一个广告,而右滑递减,递减至-1时,即不在数组范围时,重新赋值广告数组的最后一个

  1 #import "CirculateScrollview.h"
  2 
  3 #define ViewWidth self.frame.size.width
  4 #define ViewHeight self.frame.size.height
  5 #define AllImageCount self.imageArray.count-1
  6 
  7 @interface CirculateScrollview()<UIScrollViewDelegate>
  8 {
  9     NSInteger endImageCount;
 10     NSInteger oneImageCount;
 11     NSInteger secondImageCount;
 12 }
 13 @property (nonatomic,strong)UIImageView *endImageView;
 14 @property (nonatomic,strong)UIImageView *oneImageView;
 15 @property (nonatomic,strong)UIImageView *secondImageView;
 16 @property (nonatomic,strong)UIPageControl *pageCtl;
 17 
 18 @end
 19 
 20 @implementation CirculateScrollview
 21 
 22 
 23 -(id)initWithFrame:(CGRect)frame
 24 {
 25     self = [super initWithFrame:frame];
 26     if (self) {
 27         
 28     }
 29     return self;
 30 }
 31 
 32 -(NSMutableArray *)imageArray
 33 {
 34     if (!_imageArray) {
 35         _imageArray = [[NSMutableArray alloc]init];
 36     }
 37     return _imageArray;
 38 }
 39 
 40 - (void)drawRect:(CGRect)rect {
 41     self.circulateScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
 42     
 43     endImageCount = self.imageArray.count-1;
 44     oneImageCount = 0;
 45     secondImageCount = 1;
 46     
 47     self.circulateScrollView.showsHorizontalScrollIndicator = NO;
 48     self.circulateScrollView.pagingEnabled = YES;
 49     self.circulateScrollView.delegate = self;
 50     self.circulateScrollView.bounces = NO;
 51     
 52     self.circulateScrollView.contentOffset = CGPointMake(ViewWidth, 0);
 53     
 54     self.backgroundColor = [UIColor whiteColor];
 55     
 56     if (!self.imageArray.count) {
 57         NSLog(@"图片数组为空");
 58         return;
 59     }
 60     
 61     
 62     //若广告数量少于2张则不采用三屏复用技术
 63     if (self.imageArray.count<=1){
 64         self.circulateScrollView.contentSize = CGSizeMake(ViewWidth, ViewHeight);
 65         
 66         self.endImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
 67         self.endImageView.image = self.imageArray[endImageCount];
 68         [self.circulateScrollView addSubview:self.endImageView];
 69         [self addSubview:self.circulateScrollView];
 70         
 71     }else{
 72         self.circulateScrollView.contentSize = CGSizeMake(ViewWidth*3, ViewHeight);
 73         
 74         //
 75         self.endImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
 76         self.endImageView.image = self.imageArray[endImageCount];
 77         [self.circulateScrollView addSubview:self.endImageView];
 78         //
 79         self.oneImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth, 0, ViewWidth, ViewHeight)];
 80         self.oneImageView.image = self.imageArray[oneImageCount];
 81         [self.circulateScrollView addSubview:self.oneImageView];
 82         //
 83         self.secondImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth*2, 0, ViewWidth, ViewHeight)];
 84         self.secondImageView.image = self.imageArray[secondImageCount];
 85         [self.circulateScrollView addSubview:self.secondImageView];
 86         
 87         
 88         [self addSubview:self.circulateScrollView];
 89         [self pageNumControl];
 90     }
 91 
 92 }
 93 
 94 -(void)pageNumControl
 95 {
 96     self.pageCtl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ViewHeight-20, ViewWidth, 20)];
 97     self.pageCtl.backgroundColor = [UIColor lightGrayColor];
 98     self.pageCtl.currentPageIndicatorTintColor = [UIColor greenColor];
 99     self.pageCtl.pageIndicatorTintColor = [UIColor whiteColor];
100     self.pageCtl.alpha = 0.7;
101     self.pageCtl.numberOfPages = AllImageCount+1;
102     [self addSubview:self.pageCtl];
103 }
104 
105 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
106 {
107     if (scrollView.contentOffset.x == 0) {
108         endImageCount--;
109         oneImageCount--;
110         secondImageCount--;
111         if (endImageCount<0) {
112             endImageCount = AllImageCount;
113         }else if (oneImageCount<0){
114             oneImageCount = AllImageCount;
115         }
116         //适配2张图片
117         if (secondImageCount<0){
118             secondImageCount = AllImageCount;
119         }
120         //NSLog(@"endImageCount=%ld  oneImageCount=%ld  secondImageCount=%ld",endImageCount,oneImageCount,secondImageCount);
121         scrollView.contentOffset = CGPointMake(ViewWidth, 0);
122         self.endImageView.image = self.imageArray[endImageCount];
123         self.oneImageView.image = self.imageArray[oneImageCount];
124         self.secondImageView.image = self.imageArray[secondImageCount];
125     }else if(scrollView.contentOffset.x == ViewWidth*2){
126         endImageCount++;
127         oneImageCount++;
128         secondImageCount++;
129         if (endImageCount>AllImageCount) {
130             endImageCount = 0;
131         }else if (oneImageCount>AllImageCount){
132             oneImageCount = 0;
133         }
134         //适配2张图片
135         if (secondImageCount>AllImageCount){
136             secondImageCount = 0;
137         }
138         scrollView.contentOffset = CGPointMake(ViewWidth, 0);
139         self.endImageView.image = self.imageArray[endImageCount];
140         self.secondImageView.image = self.imageArray[secondImageCount];
141         self.oneImageView.image = self.imageArray[oneImageCount];
142     }
143     self.pageCtl.currentPage = oneImageCount;
144 }
145 
146 @end

 

iOS实现三屏复用循环广告

原文:http://www.cnblogs.com/fcug/p/5192074.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!