开发者

How to fade picture loading process with an UIActivityIndicatorView?

开发者 https://www.devze.com 2023-03-17 04:33 出处:网络
I have created a custom UITableViewCell. My custom cell contents an ImageView for picture of current article. On creation of 开发者_运维技巧the cell image will be loaded from Internet and displayed in

I have created a custom UITableViewCell. My custom cell contents an ImageView for picture of current article. On creation of 开发者_运维技巧the cell image will be loaded from Internet and displayed in the UIImageView. How can I fade then downloading process with an ActivityIndicatorView?

I'm sorry for this bad English :-) I use an online translator.


If I'm understanding you correctly, it seems like you want your UIImageView's (that are lazily downloaded in realtime) to fade in once they have fully downloaded. And while they are downloading, display the spinning UIActivityIndicatorView wheel.

Here is what I suggest:

1) Instead of defining the custom view in your table cell as a UIImageView specifically, just use the more generic UIView. This is because both classes (UIImageView and UIActivityIndicatorView) are subclasses and can be set as such.

2) Initially, for any and all cells, set the UIView to the UIActivityIndicatorView (don't forget to use "startAnimating" to get it to spin) and then on the callback function for the download completion, go to the appropriate cell and set that custom UIView to the downloaded UIImageView.

3) To achieve the fade in effect, look at the following code:

// Sets the image completely transparent
imageView.alpha = 0.0f;

// Animates the image to fade in fully over 1.0 second
[UIView animationWithDuration:1.0f animations:^{
    imageView.alpha = 1.0f;
}];

4) You might need to call "setNeedsDisplay" on the table cell to refresh it's subviews after setting the new image view, and before animating it.

0

精彩评论

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