PruitIgoe
PruitIgoe

Reputation: 6384

Objective-C: Adding a delay

I'm trying to add a short delay to my app. It's just the splash screen fade out so it won't affect any other functionality in the app (since nothing else is running). I've tried a variety of approaches with no success. (This is happening in viewDidLoad of a viewController):

C's sleep: ... //add the splash screen [self.view addSubview:splashScreen];

sleep(3);
[self fadeOut:splashScreen];

NSObject's performSelector (thought this would work because doesn't UIViewController inherit from NSObject?)

[self performSelector:@selector(fadeOut:) afterDelay:3];

NSTimeInterval:

 //wait 3 seconds
NSTimeInterval theTimeInterval = 3;
[self fadeOut:splashScreen withADelayOf:&theTimeInterval];

Here is fadeOut (written to work with the NSTimeInterval example)

- (void) fadeOut:(UIView *)viewToToggle withADelayOf:(NSTimeInterval* ) animDelay {

[UIView setAnimationDelay:*animDelay];

[UIView animateWithDuration:0.25 animations:^{
    viewToToggle.alpha = 0.0;
}];

}

I get the fadeOut but not the delay. Can someone nudge me in the right direction. Thanks.

Upvotes: 0

Views: 3749

Answers (4)

Sham
Sham

Reputation: 475

Do One thing: Make an uiimageview ref in the Appdelegate and in the didfinishLunching do this: // Dont assign the main viewcontroller to the windwo here.

{
img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"splash.png"]];

img.frame = CGRectMake(0, 0, 320, 480);

[self.window addSubview:img];

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(removeSplash) userInfo:nil repeats:NO];
[self.window makeKeyAndVisible];


}



- (void)removeSplash{

    [img removerFromSuperview];
    self.window.rootViewController = self.viewController;



 }

Upvotes: -1

PruitIgoe
PruitIgoe

Reputation: 6384

Tiguero and J2TheC both pointed me in the exact direction I needed to go:

Here's the code I used in case someone else needs the assist:

//add the splash screen
[self.view addSubview:splashScreen];

//fade it out with a delay
[UIView animateWithDuration:0.75 delay:3.0 options:UIViewAnimationOptionCurveEaseIn
    animations:^{
        splashScreen.alpha = 0.0;
    }
    completion:^ (BOOL finished) {
        //do something on end
}];

Upvotes: 0

tiguero
tiguero

Reputation: 11537

You should rely on this method if you want to animate some properties of your view with some delay:

+(void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

Have a look to the ref doc here.

So your code could be something like this:

[UIView animateWithDuration:0.25 delay:3.0 options: UIViewAnimationOptionCurveLinear animations:^{
    viewToToggle.alpha = 0.0;
} completion: nil];

Upvotes: 2

J2theC
J2theC

Reputation: 4452

you can try dispatch_after or animateWithDuration:delay:options:animations:completion:

double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
   //yourcode
});

or

[UIView animateWithDuration:0.175 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        //your code
    }completion:^(BOOL completed){
        //animation completion execution
}];

Upvotes: 3

Related Questions