0

I wish to update a UILabel at the exact moment that a UIImageView animation stops playing.

My attempt at this has been to use -

[self performSelector:@selector(updateLabels) withObject:nil afterDelay:imageView.animationDuration];

Where updateLabels is the the method that updates the labels.

This does not work for the first couple of animations. However, it does work after the first 3 or 4 times it has been played.

I am using imageNamed to fill the NSArray that holds the images for the animations. And from my research I have noted that imageNamed caches the objects. So I am assuming that the lag on the first 3 or 4 runs is due to the lag off the cache.

Is there anyway to overcome this? (Preferably without changing from using imageNamed, as I said it is working fine on all other animation cycles after the first 3 or 4).

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ríomhaire
  • 3,084
  • 4
  • 25
  • 40

2 Answers2

0

Add animationDidStop: method that takes action when the animation stops.

  - (void)animationDidStop:(CAAnimation*)animation finished:(BOOL)finished 
 {
    //Update your label
 }

Note don't forget to add setDelegate to self in uiimageview.animation

ThomasCle
  • 6,792
  • 7
  • 41
  • 81
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

Did you try this: (not better realization, but) // as example

in h.file:

UIImageView *im;

im .m - file:

 -(void) viewDidLoad
 {
    [super viewDidLoad];

    im = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
   UIImage *firstIm = [UIImage imageNamed:@"foo.png"];  // example pictures
   UIImage *secondIm = [UIImage imageNamed:@"bar.png"];
   im.animationImages = [NSArray arrayWithObject:firstIm, secondIm, nil];
   im.animationDuration = 1.0;
   im.animationRepeatCount = 4;
   [im startAnimating];
   [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabels) userInfo:nil repeats:YES];

}

-(void) updateLabels
{
   if (![im isAnimating]])
   {
      //Update your label
   }
}

it use -imageNamed pictures, but in this case perform not better check of end animation. May be it help you anyway.

frankWhite
  • 1,523
  • 15
  • 21
  • Thanks @frankWhite, yes I had tried something similar using `isAnimating`. But it did not work as `isAnimating` was always in the `YES` state. Similar to [this SO link](http://stackoverflow.com/questions/6583304/alternative-for-uiimageview-isanimating). – Ríomhaire Aug 21 '12 at 14:13