一、说明:
如题,我们本篇讲的是创建一个定时器。定时器它是一个对象,在指定的时间间隔触发一个事件。定时器必须在一个运行循环中。定义一个定时器对象创建一个不定期的定时器,这个定时器不执行任何操作,但是它是可用的,可以在任何你需要启动它的时候变得可用。一个定时的计时器,它是被添加到运行循环中的。
创建定时器的方式有很多,其中一个比较方便的方法是:
+ (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
本文详细介绍了如何在iOS应用中创建定时器,包括定期和不定期的定时器创建方法,以及如何在定时事件触发时执行特定任务。通过实例代码演示了如何设置定时器、触发定时事件及停止定时器,适用于iOS开发人员。
429

被折叠的 条评论
为什么被折叠?



