您的位置:首页 > 其它

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:

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: