我有三個按鈕start
stop
和pause
..My start
和stop
按鈕做工精細與下面code..but時按Pause
它暫停計時器..但是當再次按下start.IT繼續從新增加的時間...不是從暫停時間....
supoose我暫停以5秒的啓動,等待5秒鐘,然後按開始...它應該顯示5 ......但因爲我沒有在start
timer!=nill
提到(timer:)
顯示10 ..
.. 。 怎麼會被添加..
我有問題:
Pause
不工作。
-(void)start:(NSTimer *)timer
{
if(_timer==nil)
{
startDate =[NSDate date];
_timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}
if(_timer!=nil)
{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[_timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
}
}
-(void)timer:(NSTimer *)timer
{
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
NSInteger seconds = secondsSinceStart % 60;
NSInteger minutes = (secondsSinceStart/60) % 60;
NSInteger hours = secondsSinceStart/(60 * 60);
NSString *result = nil;
if (hours > 0)
{
result = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
else
{
result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
label.text=result;
NSLog(@"time interval -> %@",result);
}
-(void)stop
{
if(_timer!=nil)
{
startDate=nil;
[_timer invalidate];
_timer = nil;
}
}
-(void)pause:(NSTimer *)timer
{
pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
previousFireDate = [_timer fireDate];
[_timer setFireDate:[NSDate distantFuture]];
}
訪問此http://stackoverflow.com/questions/9975562/how-to-pause-play-nstimer –