Question

Hi a few view controllers in my app do some pretty intensive image processing at load up.

So what Id like to do is create a full screen animated image(which I already know how to do with image view) and continue animating until the view controller is completely ready for use. I know I have to create another thread for this animation(i think) and somehow have this image loop until a certain condition is TRUE? Anyone have any good tutorials to share or have some useful code. All I've been finding online is animated launch images tutorials that just go for a certain time the programmer has set..not any tutorials that explain how to do so until a certain condition is TRUE

Was it helpful?

Solution

The trick is not to think of it as a waiting cycle. You should think of it as two distinct events: loading started, and loading completed.

When loading starts, you create your image view that hosts your .gif, and display it:

[self.imageView startAnimating];
[self.view addSubview:self.imageView];

Or, if you want it to fade in:

self.imageView.alpha = 0;
[self.imageView startAnimating];
[self.view addSubview:self.imageView];
[UIView animateWithDuration:0.5 animations:^{
    self.imageView.alpha = 1.0;
}];

Then, when loading completes, you just need to fade it back out again:

[UIView animateWithDuration:0.5 animations:^{
    self.imageView.alpha = 0;
} completion:^(BOOL finished) {
    [self.imageView stopAnimating];
    [self.imageView removeFromSuperview];
}];

See this github project for how to break down a .gif into frames and animate it using UIImageView. With this approach, you don't care how long the loading takes. Your imageView will display and automatically keep looping until you explicitly remove it.

Edit:

If you want to do custom animations using UIKit, you can put your animations in an auto-repeating (and auto-reversing?) animation block:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^{
                     // Do some animation
                 } completion:nil];

Then, cancel the animation when you loading completes:

[self.view.layer removeAllAnimations];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top