iPhone: Loading data with a progress animation?
I'm creating an iPhone app and am wondering how to have a progress animation when you're loading data? I see this 开发者_如何学运维a lot in applications such as Tweetie, but haven't been able to figure it out myself.
If you are talking about loading data over a network, ASIHTTPRequest provides a way to pass a reference to a UIProgressView that it will update with accurate progress for both downloads and uploads. I highly recommend it.
If you only want the small network spinner in the statusbar you activate it with
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
...and deactivate
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
In an app I wrote for our company that performs a very long sync (by accessing a web service), I display an UIActivityIndicatorView.
On the same view, I also included a label that displays "Fetching X of Y..." The label is updated via an NSTimer. The call looks like:
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
In addition, in order for the UI to actually update, I had to spin off the code to fetch data from the webservice into its own thread.
Do you mean a progress bar, or an activity indicator? For an occurring activity, you can use UIActivityIndicatorView
.
The flow will look a little like this.
UIActivityIndivatorView *activity = [[UIActivityIndivatorView alloc] init];
//attach activity to a view or a bar
//begin activity
[activity startAnimating];
//here will be the functionality for your data gathering progress
//once your progress or activity is done
[activity stopAnimating];
Hope this helps.
精彩评论