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

Crystal Ball animation code challenge task 3 of 3

Hey guys, Im struggling with the third part of this code challenge. The prompt is: Instruct your 'imageView' to start running the animation. So far I have this coded:

  • (void)viewDidLoad { [super viewDidLoad];

    // backgroundImage is a local variable

    UIImage *backgroundImage = [UIImage imageNamed:@"background"];

    // imageView is a property

    self.imageView = [[UIImageView alloc] initWithImage:backgroundImage];

    // Add your code below! // Remember 'imageView' is a property // so refer to it as 'self.imageView'

    self.imageView.animationImages = [[NSArray alloc] initWithObjects:

[UIImage imageNamed:@"01.png"],

[UIImage imageNamed:@"02.png"],

[UIImage imageNamed:@"03.png"],nil];

self.imageView.animationDuration = 5;

  • (void)startAnimating {

}

The problem is that I have no idea how to write a method to start running the animation because it wasn't mentioned in the video. Any help will be appreciated thanks.

1 Answer

Check out the instance methods listed in the SDK: https://developer.apple.com/library/ios/documentation/uikit/reference/UIImageView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006889-CH3-DontLinkElementID_7

You'll find one there that is well-suited to start an animation sequence. :)

Ok I get that - (void)startAnimating is what you meant. The problem is I don't know how to use it in conjunction with my code. I think I'm suppose to define the function in the .h file. But how do I implement it in the .m file to have instruct my imageView property to start running the animation.

Ah, I think I understand what you're looking for now.

You only need to define functions that you write in your .h file, not functions or methods that are already established. That's the case here: the method is already written and taken care of. All you need to do is call it:

[self.imageView startAnimating];

Here's an article that shows an example implementation of the imageView animation sequence: http://www.appcoda.com/ios-programming-animation-uiimageview/. While it doesn't directly answer any questions you might have, it might highlight how it is used in the real world, outside of example-land, where things can get a bit abstract sometimes. :)

Thanks for all your help it worked!

Excellent! :)