animationuiimageviewnsoperationuiactivityindicatorviewnsinvocationoperation

Get UIImageView animation for at least one cycle before terminating


I'm new to Objective C and X-code and I'm having trouble getting my UIImageView animation to stay on screen for at least one complete cycle. I am using it as a custom wait animation for when a button is tapped and a bunch of saving and processing stuff happens. I perform those actions in a separate thread using NSOpearationQueue. Here's the first part of my code:

My animation is defined in ViewDidLoad and it's called 'myAnimation'. it's a UIImageView animation made from an array of UIImages. I set it hidden immediately and show it when I need to.

-(void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    NSArray *myAnimationArray  = [[NSArray alloc] initWithObjects:
                        [UIImage imageNamed:@"arisprite_0000.png"],
                        [UIImage imageNamed:@"arisprite_0001.png"],
                        [UIImage imageNamed:@"arisprite_0002.png"],
                        [UIImage imageNamed:@"arisprite_0003.png"],
                        [UIImage imageNamed:@"arisprite_0004.png"],
                        [UIImage imageNamed:@"arisprite_0005.png"],
                        [UIImage imageNamed:@"arisprite_0006.png"],
                        [UIImage imageNamed:@"arisprite_0007.png"],
                        [UIImage imageNamed:@"arisprite_0008.png"],
                        [UIImage imageNamed:@"arisprite_0009.png"],
                        [UIImage imageNamed:@"arisprite_0010.png"],
                        [UIImage imageNamed:@"arisprite_0011.png"],
                        [UIImage imageNamed:@"arisprite_0012.png"],
                        [UIImage imageNamed:@"arisprite_0013.png"],
                        [UIImage imageNamed:@"arisprite_0014.png"],
                        [UIImage imageNamed:@"arisprite_0015.png"],
                        [UIImage imageNamed:@"arisprite_0016.png"],
                        [UIImage imageNamed:@"arisprite_0017.png"],
                        [UIImage imageNamed:@"arisprite_0018.png"],
                        [UIImage imageNamed:@"arisprite_0019.png"],
                        [UIImage imageNamed:@"arisprite_0020.png"],
                        [UIImage imageNamed:@"arisprite_0021.png"],
                        [UIImage imageNamed:@"arisprite_0022.png"],
                        [UIImage imageNamed:@"arisprite_0023.png"],
                        [UIImage imageNamed:@"arisprite_0024.png"],
                        [UIImage imageNamed:@"arisprite_0025.png"],
                        [UIImage imageNamed:@"arisprite_0026.png"],
                        [UIImage imageNamed:@"arisprite_0027.png"],
                        [UIImage imageNamed:@"arisprite_0028.png"],
                        [UIImage imageNamed:@"arisprite_0029.png"],
                        [UIImage imageNamed:@"arisprite_0030.png"],
                        [UIImage imageNamed:@"arisprite_0031.png"],
                        [UIImage imageNamed:@"arisprite_0032.png"],
                        [UIImage imageNamed:@"arisprite_0033.png"],
                        [UIImage imageNamed:@"arisprite_0034.png"],
                        [UIImage imageNamed:@"arisprite_0035.png"],
                        [UIImage imageNamed:@"arisprite_0036.png"],
                        [UIImage imageNamed:@"arisprite_0037.png"],
                               nil];


myAnimation.animationImages = myAnimationArray;
myAnimation.animationDuration = 2.5;
    [myAnimation.layer setBorderColor:[[UIColor blackColor] CGColor]];
    [myAnimation.layer setBorderWidth:2.0];

[self.view addSubview:myAnimation];
    [myAnimation setHidden:YES];
...
...
...
}

-(IBAction)saveToYYY:(id)sender
{
// hide current view and show save screen
[self showAnimatedSaveScreen];

// show the saving screen
[self.view bringSubviewToFront:savingBackgroundView];
[self.view bringSubviewToFront:savingText];
[self.view bringSubviewToFront:myAnimation];


/* Operation Queue init */
NSOperationQueue *queue = [NSOperationQueue new];

/* Create our NSInvocationOperation to call save code, passing in nil */
NSInvocationOperation *saveToYYYOperation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(saveToYYYWithOperation)
                                    object:nil];

NSInvocationOperation *saveToXXXOperation = [[NSInvocationOperation alloc]
                                                  initWithTarget:self
                                                       selector:@selector(saveToXXXWithOperation)
                                                  object:nil];

/* Add the operation to the queue */
[saveToXXXOperation addDependency:saveToYYYOperation];
[queue addOperation:saveToYYYOperation];
[queue addOperation:saveToXXXOperation];    
}

then, in my last queue operation i have

-(void)saveToXXXWithOperation
{
... 
a bunch of processing
...

[self performSelectorOnMainThread:@selector(hideAnimatedSaveScreen) withObject:nil     waitUntilDone:YES];
}

Everything seems to be working fine, except that when these save operations are too fast, especially on iPads, myAnimation doesn't get to finish it's duration of 2.5. I'd like to play at least once before I hide my animation. Is there a way to implement this?


Solution

  • If the question is "how can I wait 2.5 seconds after I finish saving", simply have your hideAnimatedSaveScreen implementation use delayed performance (performSelector:withObject:afterDelay:). You can use timestamps ([NSDate date]) to vary that amount, learning how much time has actually elapsed since the animation started and subtracting, as desired.

    If the question is "how can I know when my animation is done", that's hard to say, since you don't actually show any animation in your code, so it's impossible to guess what you mean when you say you've got a UIImageView animation.