tau*_*per 14 iphone objective-c nstimer ios xcode4
我需要以0:0:0的方式启动NSTimer我正在做的并且我点击了一个暂停按钮,如果用户点击播放,定时器必须暂停一段时间,它必须从暂停的时间值运行.如何暂停和玩NSTimer?有帮助吗?提前致谢.
Sam*_*ish 21
尝试这个shizzle ...对我很有用
编辑
说实话,我的实际代码是这样的:
-(IBAction)clicked:(id)sender
{
if ([startStop.titleLabel.text isEqualToString:@"Start"])
{
[startStop setTitle:@"Pause" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
timerDown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
} else if ([startStop.titleLabel.text isEqualToString:@"Pause"])
{
[startStop setTitle:@"Resume" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor colorWithRed:0/255 green:0/255 blue:255/255 alpha:1.0] forState:UIControlStateNormal];
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
} else if ([startStop.titleLabel.text isEqualToString:@"Resume"])
{
[startStop setTitle:@"Pause" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
}
Run Code Online (Sandbox Code Playgroud)
我使用了UIButton并在点击时使用了这段代码.
记得把这个:
NSDate *pauseStart, *previousFireDate;
Run Code Online (Sandbox Code Playgroud)
在你的.h文件中
编辑!
这是countUp方法.忘记了down方法.是几分钟,几分钟,几小时
- (void)countUp
{
seconds += 1;
if (seconds == 60)
{
seconds = 0;
minutes++;
if (minutes == 60)
{
minutes = 0;
hours++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18112 次 |
| 最近记录: |