Objective-c 获取数字然后每秒倒数1的方法

发布于 2024-10-15 23:25:33 字数 855 浏览 7 评论 0原文

我需要一点帮助,我有一个获取诸如 50 之类的值的方法,然后将该值分配给 trackDuration,因此 NSNumber *trackDuration = 50,我希望该方法每秒从 trackDuration 的值减去 1 并更新标签,标签称为持续时间。

这是我到目前为止所拥有的;

- (void) countDown {
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

NSNumber *trackDuration = [NSNumber numberWithDouble:[[iTunes currentTrack] duration]];
while (trackDuration > 0) {
    trackDuration - 1;
    int inputSeconds = [trackDuration intValue];
    int hours =  inputSeconds / 3600;
    int minutes = ( inputSeconds - hours * 3600 ) / 60; 
    int seconds = inputSeconds - hours * 3600 - minutes * 60; 
    NSString *trackDurationString = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
    [duration setStringValue:trackDurationString];
    sleep(1);
}}

任何帮助将不胜感激,提前感谢,萨米。

i need a little help i have a method which gets value such as 50, it then assigns that value to trackDuration, so NSNumber *trackDuration = 50, i want the method to every second minus 1 from the value of trackDuration and update a label, the label being called duration.

Here's what i have so far;

- (void) countDown {
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

NSNumber *trackDuration = [NSNumber numberWithDouble:[[iTunes currentTrack] duration]];
while (trackDuration > 0) {
    trackDuration - 1;
    int inputSeconds = [trackDuration intValue];
    int hours =  inputSeconds / 3600;
    int minutes = ( inputSeconds - hours * 3600 ) / 60; 
    int seconds = inputSeconds - hours * 3600 - minutes * 60; 
    NSString *trackDurationString = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
    [duration setStringValue:trackDurationString];
    sleep(1);
}}

Any help would be much appreciated, thanks in advanced, Sami.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不即不离 2024-10-22 23:25:33

这将阻塞主线程,并且您没有分配值 trackDuration,因此它将始终保持 50

trackDuration -1;

应该是:

trackDuration--; // or trackDuration -= 1;

另外我会这样做:

- (void)startCountDown
{
    [NSTimer scheduledTimerWithInterval:1.0f target:self selector:@selector(timerHit:) userInfo:nil repeats:YES];
}
- (void)timerHit:(NSTimer *)p_timer
{
  if( trackDuration <= 1 && [p_timer isValid] )
     [p_timer invalidate];
 // track duration is an instance variable
 trackDuration--;
 // update LABEL
}

NSTimer 需要 iOS 2.x 或更高版本

This will block the main thread, and you are not assigning the value trackDuration, so it will always stay 50

trackDuration -1;

Should be:

trackDuration--; // or trackDuration -= 1;

Also I would do it like this:

- (void)startCountDown
{
    [NSTimer scheduledTimerWithInterval:1.0f target:self selector:@selector(timerHit:) userInfo:nil repeats:YES];
}
- (void)timerHit:(NSTimer *)p_timer
{
  if( trackDuration <= 1 && [p_timer isValid] )
     [p_timer invalidate];
 // track duration is an instance variable
 trackDuration--;
 // update LABEL
}

iOS 2.x or higher is required for NSTimer

霞映澄塘 2024-10-22 23:25:33

让此方法循环运行将使您的应用程序在整个运行过程中无响应 - 您甚至不会看到任何 UI 更新。相反,您应该使用间隔为一秒的 NSTimer,并在计时器触发时更新经过的时间。

Having this method run in a loop will make your app go unresponsive for the whole time it's running — you won't even see any UI updates. Instead, you should use an NSTimer with an interval of one second, and update the elapsed time when the timer fires.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文