加入定时器 轮播图

@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.

//设置代理

self.scrollView.delegate=self;

//设置scrollView的内容尺寸

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

//设置每张图片的高度与宽度等于scrollView本身的frame框架

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

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

CGFloatimgY =0;

//设置图片信息,共有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的偏移值,使其对准第一张图片的位置

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

self.scrollView.pagingEnabled=YES;

self.scrollView.showsHorizontalScrollIndicator=NO;

self.pageControl.currentPage=0;

//设置定时器,使其每一段时间执行下一页的功能

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

//把定时器加入runloop中,实现时间片轮转,不会因为拖动其他的空间而导致轮播器不播放

NSRunLoop*runLoop = [NSRunLoopcurrentRunLoop];

[runLoopaddTimer:self.timerforMode:NSRunLoopCommonModes];

}

//根据图片在scrollView中的偏移值改变scrollView的显示内容

-(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滚动的时候会自动执行这个方法

-(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被拖动的时候调用这个方法,关闭定时器,防止定时器在后台继续运行导致放手的时候图片加速跑

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

[self.timerinvalidate];

self.timer=nil;

}

//scrollView停止拖动的时候重新启动定时器并加入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];

}

//每当第一页向前翻到最后一页,或者最后一页向后放到第一页,在图片稳定下来的一瞬间,把图片的位置换到真实的位置,起到图片的轮播作用。(注:只用设置翻滚的时候为animation这个方法才会执行)

-(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滚动降速的时候调用(注:只用设置翻滚的时候为animation这个方法才会执行)

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

[selfscrollViewDidEndScrollingAnimation:self.scrollView];

}

你可能感兴趣的:(加入定时器 轮播图)