循環廣告我們在開發中曾經是熟得不能再熟了,明天整理這篇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 scrollview完成三屏復用循環廣告】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!