scrollview里面的循环的关键定时器的应用
启动定时器的两种方法
//1
//timerWithTimeInterval需要手工把timer加入到消息循环中
NSTimer
*timer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:@selector(nextImage)
userInfo:nil
repeats:YES];
NSRunLoop
*loop = [NSRunLoop currentRunLoop];
[loop addTimer:timer
forMode:NSDefaultRunLoopMode];//这个方法仅仅是提前执行timer要执行的方法
//[timer
fire];
//2
//scheduledTimerWithTimeInterval自动把timer加入到消息循环中
NSTimer
*timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:selfselector:@selector(nextImage)
userInfo:nil
repeats:YES];
//让图片自动播放- (void)nextImage{
int n = self.pageControl.currentPage;if (n == 5 - 1) {
n = 0;}else{
n = self.pageControl.currentPage + 1;}
//让scollView自己滚动
CGFloat offsetX = n * self.scrollView.frame.size.width;
[self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];}