NStimer在iOS中无法正常工作
问题描述:
我使用以下代码显示时间...我在我的viewController中滚动视图...在这里,我显示的时间以00:00.00开始(mm:ss:SS)(分钟:秒:毫秒)和目标递增毫秒,基于毫秒的秒,基于秒的分钟...但我想显示从75:00.00(毫米:SS:SS)开始的时间,我想递减毫秒,秒和分钟00:00.00(毫米:ss.SS)...我怎么能..?NStimer在iOS中无法正常工作
我已经问这所以在下面的链接... NSTimer Decrease the time by seconds/milliseconds
我我遵循代码的时间不计算(在后台也)当我拖动,并与出释放滚动视图按键...帮我在下面的代码的变化...
enter code here
@interface ViewController : UIViewController
{
IBOutlet UILabel *time;
NSTimer *stopWatchTimer;
NSDate *startDate;
NSTimeInterval secondsAlreadyRun;
}
- (void)reset:(id)sender;
- (void)onStartPressed:(id)sender;
- (void)onStopPressed:(id)sender;
enter code here
-(void)showActivity:(NSTimer *)tim
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
// Add the saved interval
timeInterval += secondsAlreadyRun;
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
static NSDateFormatter * dateFormatter = nil;
if(!dateFormatter){
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.SS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
}
NSString *timeString=[dateFormatter stringFromDate:timerDate];
time.text = timeString;
// [dateFormatter release];
}
- (void)onStartPressed:(id)sender
{
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10
target:self
selector:@selector(showActivity:)
userInfo:nil
repeats:YES];
// Save the new start date every time
startDate = [[NSDate alloc] init]; // equivalent to [[NSDate date] retain];
[stopWatchTimer fire];
}
- (void)onStopPressed:(id)sender
{
// _Increment_ secondsAlreadyRun to allow for multiple pauses and restarts
secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate];
[stopWatchTimer invalidate];
stopWatchTimer = nil;
// [startDate release];
// [self showActivity:stopWatchTimer];
}
答
您必须先注册计时器在NSRunLoopCommonModes
答
IBOutlet UILabel * result;
NSTimer * timer;
int currentTime;
- (IBAction) start;
- (IBAction) pause;
- (void)populateLabelwithTime:(int)milliseconds;
.m文件
- (void)viewDidLoad
{
currentTime = 270000000; // Since 75 hours = 270000000 milli seconds
// ..... some codes....
}
- (IBAction) start
{
timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}
-(IBAction)pause
{
[timer invalidate];
}
- (void)updateTimer:(NSTimer *)timer {
currentTime -= 10 ;
[self populateLabelwithTime:currentTime];
}
- (void)populateLabelwithTime:(int)milliseconds
{
int seconds = milliseconds/1000;
int minutes = seconds/60;
int hours = minutes/60;
seconds -= minutes * 60;
minutes -= hours * 60;
NSString * result1 = [NSString stringWithFormat:@"%@%02dh:%02dm:%02ds:%02dms", (milliseconds<[email protected]"-":@""), hours, minutes, seconds,milliseconds%1000];
result.text = result1;
}
答
定时器在某些运行循环模式下的运行循环中进行安排。只有当运行循环以其中一种模式运行时,它们才会触发。 +scheduledTimerWithTimeInterval:...
方法在默认模式下调度定时器。在滚动条被操纵时跟踪事件使用NSEventTrackingRunLoopMode
。您需要将计时器安排在适当的模式下。
您可以安排在NSRunLoopCommonModes
这是一组虚拟模式。运行循环永远不会以这种模式运行,但它们运行在该组的成员模式下。默认模式和NSEventTrackingRunLoopMode
被添加到该集,因为是NSModalPanelRunLoopMode
。
也相关(和我最大的gottcha):每个线程都有自己的RunLoop对象。 [[NSRunLoop currentRunLoop]会给你当前线程的runloop对象;并根据你的代码,如果你不在主线程上安排你的计时器,你的计时器可能永远不会打勾。 – pretzels1337 2012-09-21 19:51:44