Adding an Activity Indicator Programmatically to a View

34,703

For all those who needed this, and i know there were many...
(Made on Xcode version 4.2.1)

So...

In the AppDelegate.h add this:

@property (nonatomic, strong) UIImageView *splashView;

In the AppDelegate.m add this:

On the top of the page of cours @synthesize splashView;
And then:

- (void) splashFade
{
    splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
    splashView.image = [UIImage imageNamed:@"Default.png"];
    [_window addSubview:splashView];
    [_window bringSubviewToFront:splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationDelay:2.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:_window cache:YES];
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    splashView.alpha = 0.0;
    [UIView commitAnimations];

    //Create and add the Activity Indicator to splashView
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.alpha = 1.0;
    activityIndicator.center = CGPointMake(160, 360);
    activityIndicator.hidesWhenStopped = NO;
    [splashView addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [splashView removeFromSuperview];
}

The [UIView setAnimationDelay:2.5] is responsible for how long the splashView will be in front by the delay time you choose.

You can change the position of the indicator by changing the nubmers of x/y in:
activityIndicator.center = CGPointMake(160, 360);

At last, under the methode:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Just add this:

[self performSelector:@selector(splashFade) withObject:nil];

And there you go :)
Hope it helped.

Have a nice programming....

Share:
34,703
Slinky
Author by

Slinky

Software developer for a large music retailer

Updated on July 20, 2022

Comments

  • Slinky
    Slinky almost 2 years

    Possible Duplicate:
    Show activity indicator during application launch

    All,

    Inside my app delegate, I created an animated splash view that uses my Default.png. That all works OK but I cannot figure out how get my ActivityIndicator to display on top of the splash view. It's there just hidden by the splash view. Here is what I have and thanks:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
     //... data access stuff here ...
    
     self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    
    // ... more setup stuff here ...
    
    
    
    /****************************************************************************
     *
     *
     * Splash Screen for iPhone
     *
     ****************************************************************************/
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    
    
        splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
        splashView.image = [UIImage imageNamed:@"Default.png"];
        [self.window addSubview:splashView];
        [self.window bringSubviewToFront:splashView];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.window cache:YES];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
        splashView.alpha = 0.0;
        splashView.frame = CGRectMake(-60, -60, 440, 600);
        [UIView commitAnimations];
    
    
        //Create and add the Activity Indicator to splashView
        UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicator.alpha = 1.0;
        activityIndicator.center = CGPointMake(160, 240);
        activityIndicator.hidesWhenStopped = NO;
        [splashView addSubview:activityIndicator];
        [activityIndicator startAnimating];
    
    
    
    }
    
    
      return YES;
    }
    
  • Slinky
    Slinky over 12 years
    Yep. Get the same result. The problem may be that splashView is a UIImageView. I heard that sometimes putting subViews on top of UIImageViews doesn't work
  • Slinky
    Slinky over 12 years
    Yep, that just gives me a nice transition from splashView to the main view. When I comment it out, I still get the same result - The ActivityIndicator is hidden behind the splashView
  • Gajendra Rawat
    Gajendra Rawat over 9 years
    Hi what should the parameter in the (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
  • noobular
    noobular about 9 years
    hidesWhenStopped = NO was key for me, otherwise it would be hidden no matter what