Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Daniel Underriter
Daniel Underriter
6,794 Points

NSArray of Animated Images Not Working

I have a question regarding NSArrays and animated Images.

I am trying to create an animated imageview. I have the frames (exported from After Effects) in my bundle. However, when I set it to freeze at the last frame of the animation, it's a no-go. The issue is that I am trying to set the array like this:

    tequilaLogoArray = [NSArray arrayWithObject:[UIImage animatedImageNamed:@"tequila_" duration:2.12f]];
    self.ingredLogo.animationImages = tequilaLogoArray;
    self.ingredLogo.animationRepeatCount = 1;
    self.ingredLogo.image = [self.ingredLogo.animationImages lastObject];
    [self.ingredLogo startAnimating];

It does not stop looping, instead it just keep cycling through the frames. It only stops at the last frame when I create an array with all the frames inside of it.

Question 1: Does animatedImageNamed not conform to NSArray like imageNamed does?

Each animation has 62 frames and I have close to 100 ingredients so far, so that's a lot of arrays to list out with each frame. I was hoping this way would work with just one line of code, but maybe I'm wrong.

Question 2: If this isn't the way, would it be easier to create a bunch of arrays in a custom class of NSObject then just reference the arrays in the viewDidLoad of my detailViewController?

Thanks guys! Any help pointing me in the right direction would be great!

1 Answer

Joshua C
Joshua C
51,696 Points

The problem with the animatedImages property you're using is that there is no delegate to tell us when the animation should stop.

Try using the CAKeyFrameAnimation class to do what you need.

Here is a really helpful link on objc.io: http://www.objc.io/issue-12/animations-explained.html

One from a website called Atomic Object: http://spin.atomicobject.com/2014/08/27/animate-images-uiimageview-completion-handler/

and lastly, here is the link to the class reference in the Apple docs: https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CAKeyframeAnimation_class/index.html