ios 如何在iphone应用程序中使用进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7412795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use the progress bar in the iphone app
提问by Ravi
In my iPhone app I am downloading some data from an FTP server. To show the action I am using UIActivityIndicator
. If I put UIProgressView
there instead of UIActivityIndicator
, it will be more appropriate. How do I use UIProgressView
while downloading some data? Can anybody give me a tutorial link or example code? Thanks in advance.
在我的 iPhone 应用程序中,我正在从 FTP 服务器下载一些数据。显示我正在使用的操作UIActivityIndicator
。如果我把它放在UIProgressView
那里而不是UIActivityIndicator
,它会更合适。UIProgressView
下载一些数据时如何使用?谁能给我一个教程链接或示例代码?提前致谢。
回答by Dhaval Rathod
first you create IBOutlet in .h file
首先在 .h 文件中创建 IBOutlet
IBOutlet UIProgressView * threadProgressView;
Then in .m file in viewdidload first set progress to 0.0 and then call makeMyProgressMoving method
然后在 viewdidload 的 .m 文件中首先将进度设置为 0.0 然后调用 makeMyProgressMoving 方法
threadProgressView.progress = 0.0;
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
then add below method
然后添加下面的方法
- (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{
}
}
also give your review for answer. is it useful to you?
也给你的答案的评论。对你有用吗?
回答by Nekto
It is quite simple. You just need to set appropriate value of property progress
of UIProgressView
.
这很简单。你只需要设置的属性适当的值progress
的UIProgressView
。
In delegate of NSURLConnection
you should receive the amount of data you are waiting to download and update the progress
during downloading. Progress
is represented by a floating-point value between 0.0 and 1.0, inclusive, where 1.0 indicates the completion of the task.
在NSURLConnection
您的委托中,您应该收到等待下载的数据量并progress
在下载过程中更新。Progress
由介于 0.0 和 1.0(含)之间的浮点值表示,其中 1.0 表示任务完成。
回答by Ketan Shinde
You can display progress of progress bar with these line of code
您可以使用这些代码行显示进度条的进度
-(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);
}
回答by SachinVsSachin
One of the option is AFNetworking. AFURLConnectionOperation also allows you to easily stream uploads and downloads, handle authentication challenges, monitor upload and download progress, and control the caching behavior or requests.
选项之一是 AFNetworking。AFURLConnectionOperation 还允许您轻松流式传输上传和下载、处理身份验证挑战、监控上传和下载进度以及控制缓存行为或请求。
回答by anson
noted: self.progressionBalance.progress = 5.0/10.0;
注意:self.progressionBalance.progress = 5.0/10.0;
you must set decimal.
你必须设置十进制。