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 stopAnimating

Wondering if anyone else experienced the background animating on every other event?

I ended up adding [self.backgroundImage stopAnimating] in my motionBegan withEvent function to fix this.

Is there a better way?

// Deliver a prediction
- (void)makePrediction {
    [self.backgroundImage startAnimating];
    NSUInteger randomIndex = arc4random_uniform([self.predictionArray count]);
    self.predictionLabel.text = [predictionArray objectAtIndex:randomIndex];
}

- (void)resetPredictionLabel {
    self.predictionLabel.text = @"Ask a question";
}

// Clear the current prediction
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    predictionLabel.text = @"";
    [self.backgroundImage stopAnimating];
}

// Display new prediction
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(motion == UIEventSubtypeMotionShake) {
        [self makePrediction];
    }
}

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

That should not happen since you are starting the animation in the makePrediction method.

That was my assumption. However, in the simulator (I haven't tested on a device), the animation only fired on every other shake. Could this be a product of the simulator?

I will inspect my code further for something I may have missed, also.

Thanks.