Question

I'm rather new to iOS programming and Objective-C in general.

What I have is a bunch of images named "image0.png", "image1.png", ... ,"image47.png" that form an animated image. First I created a button that when pressed, began the animation.

@property (nonatomic, strong) IBOutlet UIImageView *imageView;

...

- (IBAction)startAnimation {
  self.imageView.animationImages = [NSArray array];
  for (int i = 0; i < 48; ++i) {
    NSString *imageName = [NSString stringWithFormat:@"image%i.png", i];
    UIImage *image = [UIImage imageNamed:imageName];
    self.imageView.animationImages = [self.imageView.animationImages arrayByAddingObject:image];
  }
  self.imageView.animationDuration = 5.0;
  [self.imageView startAnimating];
}

This worked perfectly fine, except you had to press the button to start. I wanted the animated image to begin immediately without having to press anything. What I tried to do was give the imageView a custom UIImageView class. Then in the customImageView.m file I shoved the above code into the initWithFrame: method.

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self.animationImages = [NSArray array];
    for (int i = 0; i < 48; ++i)
      self.animationImages = [self.animationImages arrayByAddingObject:[UIImage imageNamed:[NSString stringWithFormat:@"image%i.png", i]]];
    self.animationDuration = 5.0;
    [self startAnimating];
  }
  return self;
}

However, now nothing happens. I've also tried moving the [self startAnimating] line back to an IBAction, but still nothing.

Was it helpful?

Solution

Put the code you have in startAnimation into ViewDidAppear instead. It will start animating as soon as the controller's view appears.

OTHER TIPS

if you dont want to call this method on viewWillAppear. You can also do this.

 - (void)viewDidLoad{
     [self performSelector:@selector(startAnimating) withObject:nil afterDelay:0.3];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top