开发者

UIActivityIndicatorView, display during image download on current view

I have a button and, when pressed, 开发者_Go百科it downloads several images from the web in order to display them in another view. However, at the moment, when I press the button, the button goes to its highlighted state and seems to get stuck there while the images download (and essentially the next view is prepared). I'm not fussed about the button being stuck in highlighted mode (in fact I prefer it that way).

However, what I would like is for a UIActivityView to be displayed on the view where the button is displayed while the next view loads (and the images are downloaded from the web, as this takes a while)...How would I go about implementing this logically?

Thanks,

Jack


I assume you're using NSURLConnection to connect and download the images? If this is the case, you would want to use an approach like this:

In IB, place a UIActivityIndicatorView on your view where you'd like it and define its style in its Attributes panel. Tell it to Hide When Stopped. And be sure to link it with your code in the owner's header file.

UIActivityIndicatorView *indicator;

and

@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *indicator;

When you create your NSURLConnection and begin the request, tell the indicator to begin animation with [indicator startAnimation];. If you don't want your button on screen, you can remove it from the superview if you like.

Once you're done downloading, you can tell the indicator [indicator stopAnimation] (and add your button back if you had removed it earlier) in your - (void)connectionDidFinishLoading:(NSURLConnection *)connection method.

If you are downloading the images without using NSURLConnection, this of course would be different, but logically it should be the same approach. Though I agree with David that this should probably be done on a background thread


You are doing long work in the main thread. You should change your code so the download occurs in a background thread. You can change the state of the button to indicate the download is in progress, but don't holdup the main thread this way.

In the background thread, load the pictures and call a selector on the main thread which updates the UIActivtyView to indicate its progress as it downloads more pictures.


For your button problem assign the same image for highlighted state that you are assigning for normal

UIImage *newImage12 = [UIImage imageNamed:@"yourimagename"];
[deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
[deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];

For adding activity indicator

in .h file

IBOutlet UIActivityIndicatorView *indicator;

make property

add indicator in your xib and do connection.

synthesize and release in.m file

on your button click where you want to show your indicator:-

[indicator startAnimating];

where you want to stop your indicator write

[indicator stopAnimating];


You would set your indicators frame to be where you need it; center of the UIButton.

You haven't specified how you download your images, but when you start the download, use [indicator startActivity]; and when the download is complete, use [indicator stopActivity];

You can also add a setHidden:YES / setHidden:NO where you add the start/stop calls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜