小编Ris*_*abh的帖子

在iPhone中暂停秒表计时器?

我是iPhone新手NSTimer.我做了一个秒表.现在我想暂停两次之间的时间,也想暂停休息.我的代码如下.

  • 我该如何暂停时间?
  • 我怎样才能从时间停止的地方恢复?

开始时间:

startDate = [[NSDate date]retain];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                      target:self
                                                    selector:@selector(updateTimer)
                                                    userInfo:nil
                                                     repeats:YES];
Run Code Online (Sandbox Code Playgroud)

停止时间:

[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
Run Code Online (Sandbox Code Playgroud)

更新时间:

(void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;
    [dateFormatter release];
}
Run Code Online (Sandbox Code Playgroud)

iphone xcode objective-c

0
推荐指数
1
解决办法
5232
查看次数

标签 统计

iphone ×1

objective-c ×1

xcode ×1