我用NSTimer进行了45秒的倒计时,如下所示:
-(IBAction)Start {
mainInt = 45;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; }
- (void)countDown {
mainInt -= 1;
seconds.text = [NSString stringWithFormat:@"%i", mainInt];
if (mainInt == 0) {
[timer invalidate];
}
然后我在情节提要中放置了一个简单的UIProgressView。我尝试不使用代码将两者链接在一起,但这显然不是那么容易。如何将UIProgressView链接到NSTimer,以确保一旦倒数到0,进度栏将变满?
最佳答案
您可以从UIProgressView
的选择器方法更新NSTimer
- (void)countDown
{
mainInt -= 1;
seconds.text = [NSString stringWithFormat:@"%i", mainInt];
yourProgressView.progress = (45 - mainInt)/45.0f;
if (mainInt == 0)
{
[timer invalidate];
}
}