如何在iPhone应用程序中使用进度条
在我的iPhone应用程序,我正在从FTP服务器下载一些数据。 显示我使用UIActivityIndicator
。 如果我把UIProgressView
,而不是UIActivityIndicator
,这将是更合适的。 下载一些数据时如何使用UIProgressView
? 任何人都可以给我一个教程链接或示例代码? 提前致谢。
首先在.h文件中创buildIBOutlet
IBOutlet UIProgressView * threadProgressView;
然后在.m文件中的viewdidload首先设置进度为0.0,然后调用makeMyProgressMoving方法
threadProgressView.progress = 0.0; [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
然后添加下面的方法
- (void)makeMyProgressBarMoving { float actual = [threadProgressView progress]; if (actual < 1) { threadProgressView.progress = actual + ((float)recievedData/(float)xpectedTotalSize); [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO]; } else{ } }
也给你的评论回答。 对你有用吗?
这很简单。 你只需要设置适当的UIProgressView
属性progress
值。
在NSURLConnection
委托中,您应该会收到您正在等待下载的数据量,并在下载过程中更新progress
。 Progress
由0.0到1.0之间的一个浮点值表示,其中1.0表示完成任务。
你可以用这行代码显示进度条的进度
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data { if (file) { [file seekToEndOfFile]; progressView.progress = ((float)recievedData / (float) xpectedTotalSize); } [file writeData:data]; recievedData += data.length; NSLog(@"Receiving Bytes: %d", recievedData); }
其中一个select是AFNetworking。 AFURLConnectionOperation还允许您轻松地上传和下载stream,处理身份validation挑战,监视上载和下载进度以及控制caching行为或请求。
注意到:self.progressionBalance.progress = 5.0 / 10.0;
你必须设置十进制。