加入定時(shí)器 輪播圖

@property(weak,nonatomic)IBOutletUIScrollView*scrollView;

@property(weak,nonatomic)IBOutletUIPageControl*pageControl;

@property(nonatomic,strong)NSTimer*timer;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//設(shè)置代理

self.scrollView.delegate=self;

//設(shè)置scrollView的內(nèi)容尺寸

self.scrollView.contentSize=CGSizeMake(self.scrollView.bounds.size.width* (count+2),self.scrollView.bounds.size.height);

//設(shè)置每張圖片的高度與寬度等于scrollView本身的frame框架

CGFloatimgW =self.scrollView.frame.size.width;

CGFloatimgH =self.scrollView.frame.size.height;

CGFloatimgY =0;

//設(shè)置圖片信息,共有5張圖片,可是在第一張圖片前面添加最后一張圖片,在最后一張圖片添加第一張圖片,起到欺騙視覺作用

UIImageView*firstImg = [[UIImageViewalloc]init];

firstImg.image= [UIImageimageNamed:@"img_05"];

firstImg.frame=CGRectMake(0, imgY, imgW, imgH);

[self.scrollViewaddSubview:firstImg];

UIImageView*lastImg = [[UIImageViewalloc]init];

lastImg.image= [UIImageimageNamed:@"img_01"];

lastImg.frame=CGRectMake((count+1) * imgW, imgY, imgW, imgH);

[self.scrollViewaddSubview:lastImg];

for(inti =0; i

UIImageView*img = [[UIImageViewalloc]init];

img.image= [UIImageimageNamed:[NSStringstringWithFormat:@"img_%02d",i+1]];

img.frame=CGRectMake((i+1) * imgW, imgY, imgW, imgH);

[self.scrollViewaddSubview:img];

}

//初始化scrollView的偏移值,使其對準(zhǔn)第一張圖片的位置

self.scrollView.contentOffset=CGPointMake(imgW, imgY);

self.scrollView.pagingEnabled=YES;

self.scrollView.showsHorizontalScrollIndicator=NO;

self.pageControl.currentPage=0;

//設(shè)置定時(shí)器,使其每一段時(shí)間執(zhí)行下一頁的功能

self.timer= [NSTimertimerWithTimeInterval:1.5target:selfselector:@selector(nextImg)userInfo:nilrepeats:YES];

//把定時(shí)器加入runloop中,實(shí)現(xiàn)時(shí)間片輪轉(zhuǎn),不會因?yàn)橥蟿悠渌目臻g而導(dǎo)致輪播器不播放

NSRunLoop*runLoop = [NSRunLoopcurrentRunLoop];

[runLoopaddTimer:self.timerforMode:NSRunLoopCommonModes];

}

//根據(jù)圖片在scrollView中的偏移值改變scrollView的顯示內(nèi)容

-(void)nextImg{

CGFloatoffsetX =self.scrollView.contentOffset.x;

intindex = offsetX /self.scrollView.frame.size.width;

index ++;

[self.scrollViewsetContentOffset:CGPointMake(index *self.scrollView.frame.size.width,0)animated:YES];

}

//代理方法,scrollView滾動的時(shí)候會自動執(zhí)行這個(gè)方法

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{

CGFloatwidth =self.scrollView.frame.size.width;

CGFloatoffsetX =self.scrollView.contentOffset.x;

intindex = (offsetX + width *0.5) / width;

self.pageControl.currentPage= index -1;

}

//scrollView被拖動的時(shí)候調(diào)用這個(gè)方法,關(guān)閉定時(shí)器,防止定時(shí)器在后臺繼續(xù)運(yùn)行導(dǎo)致放手的時(shí)候圖片加速跑

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{

[self.timerinvalidate];

self.timer=nil;

}

//scrollView停止拖動的時(shí)候重新啟動定時(shí)器并加入runloop中

-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate{

self.timer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(nextImg)userInfo:nilrepeats:YES];

NSRunLoop*runloop = [NSRunLoopmainRunLoop];

[runloopaddTimer:self.timerforMode:NSRunLoopCommonModes];

}

//每當(dāng)?shù)谝豁撓蚯胺阶詈笠豁?,或者最后一頁向后放到第一頁,在圖片穩(wěn)定下來的一瞬間,把圖片的位置換到真實(shí)的位置,起到圖片的輪播作用。(注:只用設(shè)置翻滾的時(shí)候?yàn)閍nimation這個(gè)方法才會執(zhí)行)

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView{

CGFloatwidth =self.scrollView.frame.size.width;

CGFloatoffsetX =self.scrollView.contentOffset.x;

intindex = (offsetX + width *0.5)/width;

if(index ==count+1) {

[self.scrollViewsetContentOffset:CGPointMake(width,0)animated:NO];

}elseif(index ==0){

[self.scrollViewsetContentOffset:CGPointMake(count* width,0)animated:NO];

}

}

//scrollView滾動降速的時(shí)候調(diào)用(注:只用設(shè)置翻滾的時(shí)候?yàn)閍nimation這個(gè)方法才會執(zhí)行)

-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{

[selfscrollViewDidEndScrollingAnimation:self.scrollView];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容