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

Animation delay on CrystalBall app

I'm experiencing a 3 seconds delay on the first prediction animation on my device. It only happens on the first prediction, after that everything is fine. I have already checked the code an it runs perfectly fine on the simulator. Do you have any idea what's creating that delay?

Sounds like you have a typical device performance problem. Best way for us to help is for you to post the relevant code snippets, in this case the method that loads in the images used for animation.

6 Answers

try loading the animation images into your property in view did load. This is probably what takes so long since there are so many so if you do it in view did load it only happens once when the app starts. . and then call the actual animation methods in your button action method. Without your code I really cant say much though

//  AJMViewController.m
#import "AJMViewController.h"
#import "AJMCrystalBall.h"
#import <AudioToolbox/AudioToolbox.h> 

@interface AJMViewController ()
@end

@implementation AJMViewController {
    SystemSoundID soundEffect; 
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.crystalBall = [[AJMCrystalBall alloc] init];  
    self.backgroundImageView.animationImages = [[NSArray alloc] initWithObjects:
                                            [UIImage imageNamed:@"CB00001"],
                                            [UIImage imageNamed:@"CB00002"],
                                            [UIImage imageNamed:@"CB00003"], 
                                            //.... and so on
                                            [UIImage imageNamed:@"CB00059"],
                                            [UIImage imageNamed:@"CB00060"], nil];
    self.backgroundImageView.animationDuration = 2.0f;
    self.backgroundImageView.animationRepeatCount = 1;

    NSString *pathSound = [[NSBundle mainBundle] pathForResource:@"crystal_ball" ofType:@"mp3"];
    NSURL *soundURL = [NSURL fileURLWithPath:pathSound];
    AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &(soundEffect));
}

-(void)makePredictions {
    [self.backgroundImageView startAnimating];
    self.predictionLabel.text = [self.crystalBall randomPrediction];
    [UIView animateWithDuration:4.5f animations:^{
        self.predictionLabel.alpha = 1.0f;
    }];
    AudioServicesPlaySystemSound(soundEffect); // Lenguaje C. Play!!
}


#pragma mark - Motion Events
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    self.predictionLabel.text = nil;
    self.predictionLabel.alpha = 0.0f;
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) {
        [self makePredictions];
    }
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"motion cancelled");
}


#pragma mark - Touch Events
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.predictionLabel.text = nil;
    self.predictionLabel.alpha = 0.0f;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self makePredictions];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touches cancelled");
}

@end
//  AJMCrystalBall.m
#import "AJMCrystalBall.h"

@implementation AJMCrystalBall

-(NSArray *)predictions {
    if (_predictions == nil) {
        _predictions = [[NSArray alloc] initWithObjects:@"It is certain",
                    @"It is decidedly so", @"All the signs say YES!",
                    @"The starts are not aligned", @"My reply is no.",
                    @"It is doubtful", @"Better no to tell you", nil];
    }
    return _predictions;
}

-(NSString *) randomPrediction {
    int random = arc4random_uniform(self.predictions.count);
    return [self.predictions objectAtIndex:random];
}

@end

I'm sorry I'dont know how to post the code within the black window. Thank you so much for your help!

you can indent a new line with4 spaces and post a line of code

like this

or you can add followed by a code block followed by a closing

this is a block of code
that has multiple lines 
in it

sorry the characters you add for the code block are "```"

Thank you!! I edited the posts, now the code is readable. I hope you can take a look to the code.

someone ?