iOS實現三屏複用迴圈廣告,ios複用廣告

來源:互聯網
上載者:User

iOS實現三屏複用迴圈廣告,ios複用廣告

迴圈廣告我們在開發中已經是熟得不能再熟了,今天整理這篇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 *)scrollView106 {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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.