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
0 0
原创粉丝点击