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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9400193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:57:22  来源:igfitidea点击:

Adding an Activity Indicator Programmatically to a View

iosuiactivityindicatorviewuiapplicationdelegate

提问by Slinky

Possible Duplicate:
Show activity indicator during application launch

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

All,

全部,

Inside my app delegate, I created an animated splash view that uses my Default.png. That all works OK but I cannot figure out how get my ActivityIndicator to display on top of the splash view. It's there just hidden by the splash view. Here is what I have and thanks:

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

回答by Maor Zohar

For all those who needed this, and i know there were many...
(Made on Xcode version 4.2.1)

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

So...

所以...

In the AppDelegate.h add this:

在 AppDelegate.h 中添加:

@property (nonatomic, strong) UIImageView *splashView;

In the AppDelegate.m add this:

在 AppDelegate.m 中添加:

On the top of the page of cours @synthesize splashView;
And then:

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

The [UIView setAnimationDelay:2.5]is responsible for how long the splashView will be in front by the delay time you choose.

[UIView的setAnimationDelay:2.5]负责splashView有多长在前面由您选择的延迟时间。

You can change the position of the indicator by changing the nubmers of x/y in:
activityIndicator.center = CGPointMake(160, 360);

您可以通过更改 x/y 中的数字来更改指标的位置:
activityIndi​​cator.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....

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

回答by NeverBe

Seems you hiding (alpha -> 0.0) your splashView in 0.5sec. What you should see in this case?

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

回答by jonkroll

Have you tried:

你有没有尝试过:

[splashView bringSubviewToFront:activityIndicator];