迴圈廣告我們在開發中已經是熟得不能再熟了,今天整理這篇scrollview三屏復用廣告 原理使用scrollview里的三個imageview分別去載入不同的圖片,用少量的資源來顯示大量或不確定的廣告數量,不然如果用普通方法實現廣告,難道10個廣告用12個scrollview的contentsize
迴圈廣告我們在開發中已經是熟得不能再熟了,今天整理這篇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