以编程方式向视图添加活动指示器

时间:2012-02-22 17:49:52

标签: ios uiactivityindicatorview uiapplicationdelegate

  

可能重复:
  Show activity indicator during application launch

所有

在我的app委托中,我创建了一个使用我的Default.png的动画启动视图。一切正常,但我无法弄清楚如何让我的ActivityIndi​​cator显示在初始视图之上。它只是被飞溅视图隐藏起来。这就是我所拥有的并感谢:

- (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;
}

3 个答案:

答案 0 :(得分:22)

对于所有需要这个的人,我知道有很多...... (在Xcode版本4.2.1上制作)

所以......

在AppDelegate.h中添加:

@property (nonatomic, strong) UIImageView *splashView;

在AppDelegate.m中添加:

在cours页面的顶部@synthesize splashView;
然后:

- (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];
}

[UIView setAnimationDelay:2.5] 负责根据您选择的延迟时间将splashView放在前面多长时间。

您可以通过更改x / y中的nubmers来改变指标的位置:
activityIndi​​cator.center = CGPointMake(160,360);

最后,根据方法:

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

只需添加:

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

然后你去:) 希望它有所帮助。

有一个很好的编程......

答案 1 :(得分:0)

似乎你在0.5秒内隐藏了(alpha - > 0.0)你的splashView。在这种情况下你应该看到什么?

答案 2 :(得分:0)

你试过了吗?

[splashView bringSubviewToFront:activityIndicator];