我正在尝试实现一个持续完成10秒钟的UIProgressView。我正在使用NSTimer试图实现这一点,但我很难搞清楚。我已经查阅过UIProgressView和NSTimer的文档,以及其他已发布的问题,如果可以,请帮帮我。
我当前的实现根本不更新UIProgress View。源代码摘录如下:
- (void)updateTimer:(NSTimer *)timer
{
// sets timer
[self setTimer:[NSTimer scheduledTimerWithTimeInterval:0.25f
target:self
selector:@selector(updateTimer:)
userInfo:nil
repeats:YES]];
float newProgress = [self.timeProgressView progress] + 0.025;
[self.timeProgressView setProgress:newProgress animated:YES];
}
感谢任何帮助。
答案 0 :(得分:4)
试试这个
progressView.progress = 1;
time = 1;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f
target: self
selector: @selector(updateTimer)
userInfo: nil
repeats: YES];
-(void) updateTimer
{
if(time <= 0.0f)
{
//Invalidate timer when time reaches 0
[timer invalidate];
}
else
{
time -= 0.01;
progressView.progress = time;
}
}
答案 1 :(得分:1)
尝试设置
[self setTimer:[NSTimer scheduledTimerWithTimeInterval:0.25f
target:self
selector:@selector(updateTimer:)
userInfo:nil
repeats:YES]];
updateTimer之外的方法,除非每0.25秒调用一次。
或:
- (void)setupTimerWithTimer:(NSTimer *) timer{
[self setTimer:[NSTimer scheduledTimerWithTimeInterval:0.25f
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES]];
}
- (void)updateTimer{
float newProgress = [self.timeProgressView progress] + 0.025;
[self.timeProgressView setProgress:newProgress animated:YES];
}