Search code examples
objective-cioscore-animationcore-graphics

iOS Animated imageView disappearing


I am working on a simple game for a uni assignment, and I have completed all that is required, but I have the following irritating problem:

I have a UIImageView, (showing an image of a coin) which when the screen is tapped, animates to simulate spinning. I do this with the following code (simplified to just show relevant stuff):

- (void)spinCoin:(int)times
{
    // Create a block scoped variable to store the amount of spins.
    __block int blockTimes = times;

    // Animate the coin to spin.
    [UIView animateWithDuration:0.1
                          delay:0.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{
                         coinImageView.transform = CGAffineTransformMakeScale(0.001, 1);
                     }
                     completion:^(BOOL completed){
                         [UIView animateWithDuration:0.1
                                               delay:0.0
                                             options: UIViewAnimationCurveEaseOut
                                          animations:^{
                                              coinImageView.transform = CGAffineTransformMakeScale(1, 1);
                                          }
                                          completion:^(BOOL finished){
                                              if (blockTimes < 4) {
                                                  // Spin 4 times.
                                                  [self spinCoins:blockTimes+1];
                                              }
                                          }];
                     }];
}

This method is called in touchesEnded.

Essentially it just scales the coin image on the x axis to 0, then once that is done, scales it back to 1 (and repeats this 4 times).

This works fine despite being a bit messy (oh god blockception), but if I click the home button, then re-open the app, if I tap the screen the coin flips 1-3 times then the imageView completely disappears. There are no errors etc. in the console.

Does anyone know what could be causing this? Are there problems with Core Graphics/Animation and apps moving into out of the background?


Solution

  • The problem was that due to the UIImage references I was using for the coin images being set to 'weak', this seemed to be causing the images to be released after the application entered the background.

    Changing the attribute to 'strong' fixed the problem.

    Thanks all for your help :).