启动定时器的两种方法
//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:self selector:@selector(nextImage) userInfo:nil repeats:YES];
// NSDefaultRunLoopMode 默认模式(低于事件处理优先级)
// NSRunLoopCommonModes 通用模式 (于事件处理的优先级相同)
//让图片自动播放
- (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];
}