Objective-c 获取数字然后每秒倒数1的方法
我需要一点帮助,我有一个获取诸如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将阻塞主线程,并且您没有分配值 trackDuration,因此它将始终保持 50
应该是:
另外我会这样做:
NSTimer 需要 iOS 2.x 或更高版本
This will block the main thread, and you are not assigning the value trackDuration, so it will always stay 50
Should be:
Also I would do it like this:
iOS 2.x or higher is required for NSTimer
让此方法循环运行将使您的应用程序在整个运行过程中无响应 - 您甚至不会看到任何 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.