如何使用NSTimer
?谁能给我一步一步的指示?
答案 0 :(得分:617)
首先,我想提请你注意Cocoa / CF文档(它始终是一个很好的第一个停靠点)。 Apple文档在每篇参考文章的顶部都有一个名为“Companion Guides”的部分,其中列出了所记录主题的指南(如果存在)。例如,对于NSTimer
,the documentation列出了两个配套指南:
根据您的情况,定时器编程主题文章可能是最有用的,而线程主题是相关的,但与所记录的类最直接相关。如果你看看定时器编程主题一文,它分为两部分:
对于采用这种格式的文章,通常会概述该类及其用途,然后在如何使用它的示例代码,在本例中使用“使用计时器” “ 部分。有关“创建和调度定时器”,“停止定时器”和“内存管理”的部分。从文章中,创建一个计划的,非重复的计时器可以这样做:
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:NO];
这将创建一个在2.0秒后触发的计时器,并使用一个参数在targetMethod:
上调用self
,该参数是指向NSTimer
实例的指针。
如果您想查看该方法的更多详细信息,可以参考文档以获取更多信息,但也有关于代码的解释。
如果你想要停止一个重复的定时器,(或者在它开始之前停止非重复定时器),那么你需要保持一个指向所创建的NSTimer
实例的指针;通常这需要是一个实例变量,以便您可以在另一个方法中引用它。然后,您可以在invalidate
实例上调用NSTimer
:
[myTimer invalidate];
myTimer = nil;
nil
实例变量也是一种好习惯(例如,如果多次调用使计时器失效的方法并且实例变量未设置为nil
且{ {1}}实例已被释放,它会引发异常)。
还请注意文章底部的内存管理要点:
因为运行循环维护计时器,所以从内存管理的角度来看,通常不需要在计划后保留对计时器的引用。由于当您将方法指定为选择器时,计时器作为参数传递,可以在该方法中适当地使重复计时器无效。但是,在许多情况下,您还需要选择使计时器失效 - 甚至可能在启动之前。 在这种情况下,您需要保留对计时器的引用,以便您可以在适当时向其发送无效消息。如果您创建一个未安排的计时器(请参阅“未计划的计时器”),那么您必须保持对计时器的强引用(在引用计数环境中,保留它),以便在使用它之前不会取消分配。
答案 1 :(得分:332)
有几种使用计时器的方法:
1)预定计时器&使用选择器
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0
target: self
selector:@selector(onTick:)
userInfo: nil repeats:NO];
作为旁注,不是使用不重复的计时器并在指定的时间间隔后调用选择器,您可以使用如下的简单语句:
[self performSelector:@selector(onTick:) withObject:nil afterDelay:2.0];
这将与上面的示例代码具有相同的效果;但是如果你想每隔n次调用一次选择器,你可以使用带有重复的计时器:YES;
2)自行安排的计时器
NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 60.0];
NSTimer *t = [[NSTimer alloc] initWithFireDate: d
interval: 1
target: self
selector:@selector(onTick:)
userInfo:nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:t forMode: NSDefaultRunLoopMode];
[t release];
3)计划外计时器&使用调用
NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(onTick:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];
[inv setTarget: self];
[inv setSelector:@selector(onTick:)];
NSTimer *t = [NSTimer timerWithTimeInterval: 1.0
invocation:inv
repeats:YES];
然后,在需要时手动启动计时器:
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer: t forMode: NSDefaultRunLoopMode];
作为一个注释,onTick:方法看起来像这样:
-(void)onTick:(NSTimer *)timer {
//do smth
}
答案 2 :(得分:59)
这样的事情:
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES];
答案 3 :(得分:5)
#import "MyViewController.h"
@interface MyViewController ()
@property (strong, nonatomic) NSTimer *timer;
@end
@implementation MyViewController
double timerInterval = 1.0f;
- (NSTimer *) timer {
if (!_timer) {
_timer = [NSTimer timerWithTimeInterval:timerInterval target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
}
return _timer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)onTick:(NSTimer*)timer
{
NSLog(@"Tick...");
}
@end
答案 4 :(得分:5)
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(timerCalled) userInfo:nil repeats:NO];
-(void)timerCalled
{
NSLog(@"Timer Called");
// Your Code
}
答案 5 :(得分:2)
答案缺少一个特定的时间计时器,这是在下一个小时:
NSCalendarUnit allUnits = NSCalendarUnitYear | NSCalendarUnitMonth |
NSCalendarUnitDay | NSCalendarUnitHour |
NSCalendarUnitMinute | NSCalendarUnitSecond;
NSCalendar *calendar = [[ NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [calendar components: allUnits
fromDate: [ NSDate date ] ];
[ weekdayComponents setHour: weekdayComponents.hour + 1 ];
[ weekdayComponents setMinute: 0 ];
[ weekdayComponents setSecond: 0 ];
NSDate *nextTime = [ calendar dateFromComponents: weekdayComponents ];
refreshTimer = [[ NSTimer alloc ] initWithFireDate: nextTime
interval: 0.0
target: self
selector: @selector( doRefresh )
userInfo: nil repeats: NO ];
[[NSRunLoop currentRunLoop] addTimer: refreshTimer forMode: NSDefaultRunLoopMode];
当然,用你班级所需的方法替换“doRefresh”
尝试创建一次日历对象,并使allUnits保持静态以提高效率。
添加一个小时组件工作正常,无需午夜测试(link)