Creating Timers
2016-01-25 10:30
169 查看
一、说明:
如题,我们本篇讲的是创建一个定时器。定时器它是一个对象,在指定的时间间隔触发一个事件。定时器必须在一个运行循环中。定义一个定时器对象创建一个不定期的定时器,这个定时器不执行任何操作,但是它是可用的,可以在任何你需要启动它的时候变得可用。一个定时的计时器,它是被添加到运行循环中的。
创建定时器的方式有很多,其中一个比较方便的方法是:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
另外一个方法是:
+ (NSTimer )scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;
eg:
以上的方法是创建一个定时计时器。创建完成的时候就会启动。。
下面是创建一个不定期计时器,它需要你手动的把它添加到运行循环中。其初始化的方式有很多,其中:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
另一种方式:
+ (NSTimer )timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;
如题,我们本篇讲的是创建一个定时器。定时器它是一个对象,在指定的时间间隔触发一个事件。定时器必须在一个运行循环中。定义一个定时器对象创建一个不定期的定时器,这个定时器不执行任何操作,但是它是可用的,可以在任何你需要启动它的时候变得可用。一个定时的计时器,它是被添加到运行循环中的。
创建定时器的方式有很多,其中一个比较方便的方法是:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
另外一个方法是:
+ (NSTimer )scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;
eg:
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong) NSTimer *paintingTimer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self startPainting]; } - (void)paint:(NSTimer *)paramTimer { NSLog(@"Painting"); } - (void)startPainting { // Here is a selector tha we can to call SEL selectorToCall = @selector(paint:); NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; [invocation setTarget:self]; [invocation setSelector:selectorToCall]; // Start a schedule timer now self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 invocation:invocation repeats:YES]; } - (void)stopPainting { if(self.paintingTimer != nil){ [self.paintingTimer invalidate]; NSLog(@"定时器停止..."); } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self stopPainting]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
以上的方法是创建一个定时计时器。创建完成的时候就会启动。。
下面是创建一个不定期计时器,它需要你手动的把它添加到运行循环中。其初始化的方式有很多,其中:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong) NSTimer *paintingTimer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(paint2) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode]; } - (void)paint2 { NSLog(@"Painting"); } - (void)stopPainting { if(self.paintingTimer != nil){ [self.paintingTimer invalidate]; NSLog(@"定时器停止..."); } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self stopPainting]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
另一种方式:
+ (NSTimer )timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation )invocation repeats:(BOOL)yesOrNo;
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong) NSTimer *paintingTimer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self startPainting]; } - (void)paint:(NSTimer *)paramTimer { NSLog(@"Painting"); } - (void)startPainting { // Here is a selector tha we can to call SEL selectorToCall = @selector(paint:); NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; [invocation setTarget:self]; [invocation setSelector:selectorToCall]; self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 invocation:invocation repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode]; } - (void)stopPainting { if(self.paintingTimer != nil){ [self.paintingTimer invalidate]; NSLog(@"定时器停止..."); } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self stopPainting]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
相关文章推荐
- python废话四:python的import引入模块
- c# Console application Open/Get Url by Browser
- Educational Codeforces Round 5 :(codeforces 616)
- 【ExtJS4学习笔记03】Ajax远程访问数据源示例
- Dom9(美女时钟)
- 脏读、不可重复读 共享锁、悲观锁
- Android 手机配置移植 数据库 sqlite3
- BloomFilter算法概述
- 狡猾的商人 [bzoj1202,HNOI2005]
- BloomFilter算法概述
- html5解决大文件断点续传
- POJ 1410 Intersection(计算几何)
- Arc gis api for js比例尺,书签
- Android热补丁的一点总结
- JavaScript学习笔记
- KVO 与 KVC
- 指针链表总结
- Android如何执行一个不确定当前处于在子线程还是主线程中的UI操作
- iOS状态栏 导航条 tabbar高度
- supervisor的基本使用