标签:
NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象。可以按照一定的时间间隔,将制定的信息发送给目标对象。并更新某个对象的行为。你可以选择在未来的某个时间将它停止、开启、甚至销毁。
1、NSTimer的创建
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
注意,这两种方法有不同;
用第一种方法需要手动addTimer:forMode: 将timer添加到一个runloop中:
如:
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:selfselector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
第二种scheduled方法将以默认mode直接添加到当前的runloop:
如:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
几个参数:
TimerInterval : 执行之前等待的时间
target : 需要执行方法的对象
selector : 需要执行的方法
repeats : 是否需要循环
2、释放方法
[ timer invalidate];
//如果之后定时器不再用的话,这里最好设置为nil;如果继续使用,则不需要设置
timer = nil;
3、定时器的暂停/重启
上述的关闭定时器的方法是永久性的,在暂停计时器的时候不可以用上述的停止方法。
//暂停定时器(暂时关闭)
[timer setFireDate:[NSDate distantFuture] ] ;
//开启定时器
[timer setFireDate:[NSDate distantPast] ];
demo:
1 @interface ViewController ()<UIScrollViewDelegate>{ 2 3 UIPageControl *pageCtrl; 4 UIScrollView *scrollView; 5 NSArray *imageArray; 6 7 NSTimer *timer; 8 int lastPage; 9 int currentPage; 10 11 } 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 // Do any additional setup after loading the view, typically from a nib. 20 float width = self.view.frame.size.width; 21 22 //创建UIScrollView 23 scrollView = [[UIScrollView alloc] initWithFrame:(CGRect){0,20,width,160}]; 24 scrollView.delegate = self; 25 scrollView.pagingEnabled = YES; 26 scrollView.contentSize = CGSizeMake(width*5, 160); 27 scrollView.showsHorizontalScrollIndicator = NO; 28 scrollView.showsVerticalScrollIndicator = NO; 29 [self.view addSubview:scrollView]; 30 31 //开启定时器方法 32 [self timeOn]; 33 34 //创建UIPageControl 35 pageCtrl = [[UIPageControl alloc] initWithFrame:(CGRect){0,160,width,20}]; 36 [self.view addSubview:pageCtrl]; 37 pageCtrl.numberOfPages = 5; 38 pageCtrl.currentPageIndicatorTintColor = [UIColor grayColor]; 39 pageCtrl.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1]; 40 [pageCtrl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside]; 41 42 43 float imageWidth = width-15; 44 float imageHeight = 150; 45 46 imageArray =@[@"tu3", @"tu1", @"tu2", @"tu4", @"tu5"]; 47 48 //遍历imageArray数组,添加图片到UIScrollView 49 for (int i=0; i<imageArray.count; i++) { 50 NSString *image = [imageArray objectAtIndex:i]; 51 52 ScrollPageView *images = [[ScrollPageView alloc] initWithFrame:(CGRect){4+width*0.5*i,3,imageWidth,imageHeight} setImage:[UIImage imageNamed:image]]; 53 [scrollView addSubview:images]; 54 } 55 } 56 57 //实现图片自动切换 58 - (void)changeImage{ 59 currentPage = lastPage+1; 60 [scrollView setContentOffset:(CGPoint){currentPage*375,0} animated:YES]; 61 lastPage =currentPage; 62 if (lastPage>3) { 63 lastPage =-1; 64 } 65 } 66 67 - (void)scrollViewDidScroll:(UIScrollView *)ScrollView{ 68 int page = scrollView.contentOffset.x/scrollView.frame.size.width; 69 pageCtrl.currentPage = page; 70 } 71 72 //将要开始拖拽关闭定时器 73 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 74 [self timeOff]; 75 } 76 77 //结束拖拽开启定时器 78 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 79 [self timeOn]; 80 } 81 82 //开启定时器 83 - (void)timeOn{ 84 85 timer = [NSTimer scheduledTimerWithTimeInterval:1 86 target:self 87 selector:@selector(changeImage) 88 userInfo:nil 89 repeats:YES]; 90 91 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 92 } 93 94 //关闭定时器 95 - (void)timeOff{ 96 97 [timer invalidate]; 98 timer = nil; 99 } 100 101 - (void)pageAction:(UIPageControl*)page{ 102 int pageNum =(int)pageCtrl.currentPage; 103 CGSize size = scrollView.frame.size; 104 [scrollView setContentOffset:(CGPoint){(pageNum+1)*size.width,0} animated:YES]; 105 [timer invalidate]; 106 }
ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换
标签:
原文地址:http://www.cnblogs.com/0320y/p/5088270.html