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

8

可能重复:
在应用程序启动期间显示活动指示器

大家好,

在我的应用程序委托中,我创建了一个使用Default.png的动画闪屏视图。这一切都正常工作,但我无法弄清楚如何在闪屏视图上方显示我的ActivityIndicator。它存在,只是被闪屏视图隐藏了。以下是我的代码,谢谢:

- (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个回答

22

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

所以...

在AppDelegate.h文件中添加以下内容:

@property (nonatomic, strong) UIImageView *splashView;

在AppDelegate.m文件中添加以下内容:

在页面顶部当然要加上 @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的值来实现:
activityIndicator.center = CGPointMake(160, 360);

最后,在该方法下面:

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

只需要添加这个:

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

就是这样 :)
希望有所帮助。

祝您编程愉快....


嗨,请问(void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context中的参数应该是什么? - Gajendra Rawat
hidesWhenStopped = NO 对我来说很关键,否则无论如何它都会被隐藏。 - noobular

0

你尝试过了吗:

[splashView bringSubviewToFront:activityIndicator];

是的,得到了相同的结果。问题可能在于splashView是UIImageView。我听说有时将子视图放在UIImageView上面不起作用。 - Slinky

0

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


是的,这只是让我从闪屏视图过渡到主视图的一个不错的转换。当我将其注释掉时,我仍然得到相同的结果 - 活动指示器被隐藏在闪屏视图后面。 - Slinky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接