iOS scrollview實(shí)現(xiàn)三屏復(fù)用循環(huán)廣告
循環(huán)廣告我們?cè)陂_發(fā)中已經(jīng)是熟得不能再熟了,今天整理這篇scrollview三屏復(fù)用廣告。
原理使用scrollview里的三個(gè)imageview分別去加載不同的圖片,用少量的資源來顯示大量或不確定的廣告數(shù)量,不然如果用普通方法實(shí)現(xiàn)廣告,難道10個(gè)廣告用12個(gè)scrollview的contentsize去做,豈不是太浪費(fèi)資源了
代碼如下,實(shí)現(xiàn)所有數(shù)量的循環(huán)廣告,當(dāng)廣告只有一個(gè)時(shí),僅采用單圖顯示,>=2個(gè)廣告時(shí),自動(dòng)采用三屏復(fù)用
這里添加圖片的方式是通過網(wǎng)絡(luò)請(qǐng)求,更新服務(wù)器上的廣告,如果僅使用本地廣告,可以將.m文件里的全部圖片的添加方式
如:
self.endImageView.image = self.imageArray[endImageCount];
修改為
self.endImageView.image = [UIImage imageNamed:self.imageArray[endImageCount]];
然后在使用該類時(shí),直接將本地圖片的名字用數(shù)組傳過去就行了,如
cview.imageArray = [[NSMutableArray alloc]initWithObjects:@"圖片1",@"圖片2",@"圖片3", nil];
或者不改則使用方法如
NSArray *imageArr = [[NSArray alloc]initWithObjects:@"banner_理財(cái).jpg",@"banner_惠普",@"banner_炒股", nil]; for (int i=0; i<3; i++) { UIImage *cirImage1 = [UIImage imageNamed:imageArr[i]]; [cirScrollView.imageArray addObject:cirImage1]; }
如果圖片給的是地址那可以用imageWithURL這個(gè)方法來獲取圖片。
下面講從服務(wù)器獲取的廣告方式,請(qǐng)求服務(wù)器圖片及解析這里就不講了,僅從獲取到的data數(shù)據(jù)后開始。
先新建一個(gè)類繼承UIView。
.h文件
#import <UIKit/UIKit.h> @interface CirculateScrollview : UIView @property (nonatomic,strong)NSMutableArray *imageArray;//圖片數(shù)組 @property (nonatomic,strong)UIScrollView *circulateScrollView;//廣告 /* 三屏復(fù)用廣告 適用范圍:網(wǎng)絡(luò)請(qǐng)求或固定本地的廣告圖片 適用所有數(shù)量廣告,廣告>=2時(shí)自動(dòng)采用三屏復(fù)用技術(shù) 使用方法:例 在需要添加廣告的控制器里面 CirculateScrollview *cview = [[CirculateScrollview alloc]initWithFrame:CGRectMake(0, 20, 320, 200)]; for (int i=0; i<3; i++) { UIImage *image = [UIImage imageNamed:@"旅行圖1"];//傳進(jìn)圖片名字方式 //UIImage *image = UIImage imageWithData:data];//傳進(jìn)data數(shù)據(jù)圖片方式將服務(wù)器請(qǐng)求到的data數(shù)據(jù)圖片轉(zhuǎn)換成image形式再傳輸 [cview.imageArray addObject:image]; } [self.view addSubview:cview]; */ /* 圖片轉(zhuǎn)換NSData方法 測(cè)試可用 NSData * data = UIImageJPEGRepresentation(image, 1); */ @end
.m文件
實(shí)現(xiàn)方法是這三個(gè)成員變量,用來讀取傳輸過來的圖片在數(shù)組中的位置,三屏復(fù)用里,我們顯示的位置是scrollview的中間位置,左邊廣告是全部廣告的最后一個(gè),中間顯示第一個(gè),右邊的顯示第二個(gè),然后根據(jù)左滑成員變量遞增,當(dāng)變量遞增到超過廣告總數(shù)時(shí),重新賦值第一個(gè)廣告,而右滑遞減,遞減至-1時(shí),即不在數(shù)組范圍時(shí),重新賦值廣告數(shù)組的最后一個(gè)
#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;//中間圖片[當(dāng)前看到的圖片] 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(@"圖片數(shù)組為空"); return; } //若廣告數(shù)量少于2張則不采用三屏復(fù)用技術(shù) 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; } } //重新加載顯示當(dāng)前位置的圖片 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS等待時(shí)動(dòng)畫效果的實(shí)現(xiàn)
查詢時(shí)間有長(zhǎng)有短,為了增強(qiáng)用戶體驗(yàn)度,目前用的比較多的手段之一是查詢等待時(shí)添加一個(gè)動(dòng)態(tài)等待效果,這篇文章主要介紹IOS等待時(shí)動(dòng)畫效果的實(shí)現(xiàn),有需要的朋友可以參考下2015-08-08IOS UIWebView獲取404、504等錯(cuò)誤問題解決方案
這篇文章主要介紹了IOS UIWebView獲取404、504等錯(cuò)誤問題的相關(guān)資料,并對(duì)相應(yīng)的錯(cuò)誤問題提出相應(yīng)的解決方案,需要的朋友可以參考下2016-11-11cmake ios終端下執(zhí)行提示錯(cuò)誤 iOS version not found, tested: [5.0;5.1;6
這篇文章主要介紹了cmake ios終端下執(zhí)行提示錯(cuò)誤 iOS version not found, tested: [5.0;5.1;6.0;6.1;7.0;8.3]的解決方案的相關(guān)資料,需要的朋友可以參考下2016-10-10iOS開發(fā)避免安全隱患的要點(diǎn)總結(jié)
在本篇文章里小編給各位整理了關(guān)于iOS開發(fā)如何避免安全隱患的知識(shí)點(diǎn)總結(jié),需要的朋友們學(xué)習(xí)下。2019-07-07iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息
這篇文章主要介紹了iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11IOS 使用NSAssert()和NSParameterAssert調(diào)試程序
這篇文章主要介紹了IOS 使用NSAssert()和NSParameterAssert調(diào)試程序的相關(guān)資料,需要的朋友可以參考下2017-06-06ios開發(fā):一個(gè)音樂播放器的設(shè)計(jì)與實(shí)現(xiàn)案例
本篇文章主要介紹了ios開發(fā):一個(gè)音樂播放器的設(shè)計(jì)與實(shí)現(xiàn)案例,具有一定的參考價(jià)值,有需要的小伙伴可以參考下。2016-11-11