Search code examples
objective-cdelay

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.


Solution

  • 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];