3
我有一个NSTimeInterval这基本上是在后台运行的时间间隔计算
NSTimeInterval interval = [date1 timeIntervalSinceDate: [NSDate date]];
我希望此计算,以在后台始终运行时,我在一个视图X,因此可以显示一个计时器在UILabel中倒数..我该怎么做?这就像在groupon iphone应用程序,但它不显示它的详细信息秒钟
我有一个NSTimeInterval这基本上是在后台运行的时间间隔计算
NSTimeInterval interval = [date1 timeIntervalSinceDate: [NSDate date]];
我希望此计算,以在后台始终运行时,我在一个视图X,因此可以显示一个计时器在UILabel中倒数..我该怎么做?这就像在groupon iphone应用程序,但它不显示它的详细信息秒钟
您可以使用NSTimer获取以设定的时间间隔调用的方法(但是,这全部在主线程上执行,而不是在后台线程上):
- (void)setupTimer {
//Start a timer to call update updateInterval: every 1 second
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateInterval:)
userInfo:nil
repeats:YES];
}
- (void)updateInterval:(NSTimer *)timer {
NSTimeInterval interval = [date1 timeIntervaleSinceDate:[NSDate date]];
//Do other things like updating the label
}
so this updateInterval will be called every 1 second? – aherlambang 2011-03-31 06:01:04
正确,重复间隔也可以小于1. – 2011-03-31 06:05:32
scheduledTimerWithTimerInterval未列出http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer。 html – aherlambang 2011-03-31 06:28:01