Show activity indicator during application launch

后端 未结 4 1043
花落未央
花落未央 2020-12-21 07:13

I am trying to add an activity indicator during startup. I did have a launch image, but I\'d rather have just an indicator alone. I added the following to my app delegate, b

相关标签:
4条回答
  • 2020-12-21 07:25

    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....

    0 讨论(0)
  • 2020-12-21 07:28

    I don't think this is possible from looking at the docs for UIApplicationDelegate

    The earliest your app is called is in

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

    Which as the name suggests is when your app hasFinishedLaunching

    The reason nothing happens when you show and hide the indicator is to do with threading. The user interface will only update at the end of your method where the net result of your changes will be that the indicator is hidden.

    0 讨论(0)
  • 2020-12-21 07:33

    This isn't possible (as @Paul.S is saying). However, you can simulate this by adding a (static, so it's not animating) Default.png-splash-screen to your project.

    In my opinion, though, you should rather invest in a better performance. For example: do you link your app with unused frameworks (eg. QuartzCore, MapKit) or something.
    Starting an app should take 2 seconds max. At this time, you don't really need to show your user an activity-indicator. If it does take longer, you should take a look at what makes it so slow, rather than trying to hide it.

    0 讨论(0)
  • 2020-12-21 07:42

    Another solution is to let the launchimage load and when your ViewController loads at the viewDidLoad, use overlay + indicator on top of your view with alpha below 1:

    UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:baseView];
    [baseView setBackgroundColor:[UIColor blackColor]];
    baseView.userInteractionEnabled = NO;
    baseView.alpha = 0.4;
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    indicator.center = self.view.center;
    [self.view addSubview:indicator];
    [indicator bringSubviewToFront:self.view];
    [indicator startAnimating];
    

    Now comes the fun part, use delegate to remove the indicator + overlay when all your background threads finish to load up and simple call a method via your delegate:

    - (void) onFinishLoading{
            [indicator stopAnimating];
            [baseView removeFromSuperview];
    }
    
    • In order to stopAnimation and removeFromSuperView you might want to make baseView and indicator property + @synthesize
    0 讨论(0)
提交回复
热议问题