Chase Roberts
Chase Roberts

Reputation: 9386

iOS image startAnimating isn't animating (most of the time)

I have an animation that is not animating for some reason.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.nameLabel.text = [self.name uppercaseString];
    self.gradient1.alpha = .9;
    self.gradient1.image = [UIImage imageNamed:@"heart.png"];
    self.gradient1.animationImages = [[NSArray alloc] initWithObjects:
                                      [UIImage imageNamed:@"gradiant1.png"],
                                      [UIImage imageNamed:@"gradiant2.png"],
                                      ...
                                      [UIImage imageNamed:@"gradiant26.png"], nil];
    self.gradient1.animationDuration = .5;
    self.gradient1.animationRepeatCount = 1;

    [self updateStats:nil];

}

-(void)updateStats:(NSTimer*)timer{
Utilities *utility = [[Utilities alloc] init];
[utility getDataWithSSID:self.ssID completion:^(bool *finished, NSArray *objects) {
    if (finished){
        ...
        [self.gradient1 startAnimating];
    }
    self.twoSecTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self
                                                      selector:@selector(updateStats:)
                                                      userInfo:nil
                                                       repeats:NO];

}];

}

Sometimes it animates once or twice and then stops even though the timer continues to hit over and over. Other times it just doesn't animate at all. However, I have a couple placeholder buttons that I put on the storyboard that aren't tied to anything and whenever I press one of the buttons it animates the next time the timer hits (Actually whenever I hit the screen even though I don't have any sort of gesture recognizers..). Also, when I add in all the other garbage I want to do in this ViewController it animates every other time it's being called. So I think it's something deeper than me forgetting so set some property.

Upvotes: 0

Views: 313

Answers (1)

Till
Till

Reputation: 27597

It might be a better idea to move the animation trigger to a moment when that view is actually visible - hence to viewDidAppear:.

Also your way of creating a recursive timer scheduling makes me shiver. I guess it works fine for you but I would definitely suggest to use the repeats flag set to YES instead.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.nameLabel.text = [self.name uppercaseString];
    self.gradient1.alpha = .9;
    self.gradient1.image = [UIImage imageNamed:@"heart.png"];
    self.gradient1.animationImages = [[NSArray alloc] initWithObjects:
                                      [UIImage imageNamed:@"gradiant1.png"],
                                      [UIImage imageNamed:@"gradiant2.png"],
                                      ...
                                      [UIImage imageNamed:@"gradiant26.png"], nil];
    self.gradient1.animationDuration = .5;
    self.gradient1.animationRepeatCount = 1;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //animate right away...
    [self updateStats:nil];
    //trigger new animations every two seconds
    self.twoSecTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self
                                                      selector:@selector(updateStats:)
                                                      userInfo:nil
                                                       repeats:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self stopUpdatingStats];
}

-(void)stopUpdatingStats
{
    [self.twoSecTimer invalidate];
}

-(void)updateStats:(NSTimer*)timer
{
    NSLog(@"updating");
    [self.gradient1 startAnimating];
}

Upvotes: 1

Related Questions