Creating Timers
来源:互联网 发布:java 强制停止线程 编辑:程序博客网 时间:2024/06/05 18:12
一、说明:
如题,我们本篇讲的是创建一个定时器。定时器它是一个对象,在指定的时间间隔触发一个事件。定时器必须在一个运行循环中。定义一个定时器对象创建一个不定期的定时器,这个定时器不执行任何操作,但是它是可用的,可以在任何你需要启动它的时候变得可用。一个定时的计时器,它是被添加到运行循环中的。
创建定时器的方式有很多,其中一个比较方便的方法是:
+ (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
- Creating Timers
- 6.15 Creating Timers
- Timers
- System.Timers
- Multimedia Timers
- jQuery Timers
- .Net Timers
- Server Timers, Windows Timers, and Thread Timers的对比
- The differen between Server Timers, Windows Timers, and Thread Timers
- 5 kinds of timers
- dynamic timers 的list
- System.Timers.Timer
- Jquery Timers 插件
- System.Timers.Timer用法
- 关于System.Timers.Timer
- jquery.timers使用说明
- Not enough timers available
- jquery.timers使用说明
- 【ExtJS4学习笔记03】Ajax远程访问数据源示例
- 关于java ssh找不到驱动的问题
- 狡猾的商人 [bzoj1202,HNOI2005]
- IntelliJ IDEA像Eclipse一样打开多个项目
- Linux防火墙(iptables)的开启与关闭
- Creating Timers
- Python--多进程
- jackson 实体转json 为NULL或者为空不参加序列化
- Tsar开源:淘宝内部大量使用的系统监控工具
- mysql中float,double,decimal的差别
- Android sqlite数据库并发访问问题处理
- Nsight调试断点不停
- mybatis删除关键字数组对应项
- Java调用Runtime.getRuntime().exec(commandStr)来调用cmd执行指令