【问题标题】:Adding an Activity Indicator Programmatically to a View [duplicate]以编程方式将活动指示器添加到视图 [重复]
【发布时间】:2012-03-13 02:23:12
【问题描述】:

可能重复:
Show activity indicator during application launch

全部,

在我的应用程序委托中,我创建了一个使用我的 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;
}

【问题讨论】:

    标签: ios uiactivityindicatorview uiapplicationdelegate


    【解决方案1】:

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

    【讨论】:

    • 是的,这只是给了我从 splashView 到主视图的良好过渡。当我将其注释掉时,我仍然得到相同的结果 - ActivityIndi​​cator 隐藏在 splashView 后面
    【解决方案2】:

    你试过了吗:

    [splashView bringSubviewToFront:activityIndicator];
    

    【讨论】:

    • 是的。得到相同的结果。问题可能是 splashView 是 UIImageView。听说有时候把 subViews 放在 UIImageViews 上面是行不通的
    【解决方案3】:

    对于所有需要这个的人,我知道有很多...
    (基于 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的数值来改变指标的位置:
    activityIndi​​cator.center = CGPointMake(160, 360);

    最后,在方法下:

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

    只需添加这个:

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

    你去吧:)
    希望对您有所帮助。

    祝你编程愉快......

    【讨论】:

    • 嗨,(void)startupAnimationDone 中的参数应该是什么:(NSString *)animationID 完成:(NSNumber *)finished context:(void *)context
    • hidesWhenStopped = NO 对我来说是关键,否则无论如何它都会被隐藏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多