-2

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?

benzo
  • 160
  • 1
  • 6
  • You don't actually show any UIImageView or any animation in your code, so it's very difficult to answer. – matt Dec 30 '12 at 03:18

1 Answers1

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • added where myAnimation is defined (viewDidLoad). is my question more clear now? – benzo Dec 31 '12 at 04:07
  • If that's the kind of animation you're going to do, then I think my answer above is a good one. Simple UIImageView image animation doesn't give off any signal when we reach the end of the image array. You'd have much more flexibility and power, of course, if you did real animation. – matt Dec 31 '12 at 17:02
  • great, thanks. i'm new and learning and exploring so this kind of simple of animation will suffice for now in my prototype app. for the final version of my app, i'd like to explore what you're suggesting. are you implying i should research "core animation?" – benzo Jan 02 '13 at 18:44
  • Not particularly, just real animation generally. I've written a book that might help you: http://www.apeth.com/iOSBook/ch17.html – matt Jan 02 '13 at 18:47
  • (The point being that real animation does send you a message when the animation is over, which is what you're really after.) – matt Jan 02 '13 at 19:09