Ios 以编程方式将活动指示器添加到视图

Ios 以编程方式将活动指示器添加到视图,ios,uiactivityindicatorview,uiapplicationdelegate,Ios,Uiactivityindicatorview,Uiapplicationdelegate,可能重复: 全部, 在我的AppDelegate中,我创建了一个使用我的Default.png的动画启动视图。一切正常,但我不知道如何让我的ActivityIndicator显示在启动视图的顶部。它就在那里,就在飞溅的景色下。这是我所拥有的,谢谢你: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //... data

可能重复:

全部,

在我的AppDelegate中,我创建了一个使用我的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;
}
看起来你在0.5秒内隐藏了(alpha->0.0)你的splashView。在这种情况下,您应该看到什么?

您是否尝试过:

[splashView bringSubviewToFront:activityIndicator];

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

所以

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

@property (nonatomic, strong) UIImageView *splashView;
在AppDelegate.m中添加以下内容:

@property (nonatomic, strong) UIImageView *splashView;
在cours@View页面顶部
然后:

- (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(160360)

最后,在该方法下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
只需添加以下内容:

[self performSelector:@selector(splashFade) withObject:nil];
接下来就是:)
希望有帮助


有一个很好的编程…

是的。得到同样的结果。问题可能是splashView是UIImageView。我听说有时候将子视图放在UIImageViews的顶部是不起作用的,这给了我从splashView到主视图的一个很好的过渡。当我注释它时,我仍然得到相同的结果-ActivityIndicator隐藏在splashViewHi后面(void)startupAnimationDone:(NSString*)animationID finished:(NSNumber*)finished context:(void*)contexthidesWhenStopped=否对我来说是关键,否则它将被隐藏