1

我有一个有 96 个插槽的滑块,我需要在 60 秒内将滑块从 0 逐步移动到 95。我应该使用间隔(60/96)和 96 次重复的 NSTimer,还是有更好的解决方案?

4

2 回答 2

1

这可能是最好的方法。应该在那个间隔内NSTimer表现得相当一致,它只会在每 1/10 秒左右调用它时才开始变得不可靠,或者更快。

但是,如果它的行为不像您希望的那样,请进行一些解释:

它不会是完美的,因为实际上每个间隔NSTimer都没有它的滴答事件。相反,它受其线程的运行循环的支配,直到它的间隔到期后一段时间才能调用您的方法。然后将其与调用屏幕更新相结合,这也不是锁步。NSTimer@selector

它的准确性主要取决于您在运行循环中还做了什么……如果您的设备的小大脑中没有发生太多事情,那么您的滑块应该会像您希望的那样移动。

编辑:您也可以考虑使用间隔更长的 NSTimer,并使用 UIView 的 animateWithDuration... 方法使其看起来平滑?

于 2012-01-12T18:08:10.397 回答
1
            aTimer = [NSTimer timerWithTimeInterval:(1.0) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

            NSRunLoop *runner = [NSRunLoop currentRunLoop];
            [runner addTimer:aTimer forMode: NSDefaultRunLoopMode];

        - (void)timerFired:(NSTimer*)theTimer {

                  slider.maximumValue=totaltime;

                if(slider.value ==totaltime)
                { 
                    [theTimer invalidate];
                   //terminate the loop
        }
    else
    {
    slider.value=slider.value+1;
    }
    }

//timer runs continuously run if condition is true
于 2012-08-31T08:56:54.513 回答