ios - 以编程方式向 View 添加事件指示器

标签 ios uiactivityindicatorview uiapplicationdelegate

<分区>

Possible Duplicate:
Show activity indicator during application launch

全部,

在我的应用委托(delegate)中,我创建了一个使用我的 Default.png 的动画启动 View 。一切正常,但我无法弄清楚如何让我的 ActivityIndi​​cator 显示在启动 View 的顶部。它只是隐藏在启动 View 中。这是我所拥有的,谢谢:

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

最佳答案

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

所以...

在 AppDelegate.h 中添加:

@property (nonatomic, strong) UIImageView *splashView;

在 AppDelegate.m 中添加:

类(class)页面顶部@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];

就这样吧:)
希望对您有所帮助。

祝你编程顺利....

关于ios - 以编程方式向 View 添加事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9400193/

相关文章:

ios - Apple Developer - 配置文件显示无效状态

ios - [iOS]如何将视频转换为实时照片?

iphone - UIAlertView 与 UIActivityIndi​​cator 有点矛盾

ios - 在dispatch_async期间UIActivityIndi​​catorView?

objective-c - 应用程序应该在应用程序启动结束时有一个 Root View Controller wait_fences : failed to receive reply: 10004003

ios - CellForRowAtIndexPath 不工作

ios - 不同的行为 - iOS 6 和 iOS 7 - 困惑

iOS 应用内购买 - 使用超时是否明智?

ios - 在 AppDelegate.m 上设置 label.text

ios - 将 AppDelegate Shared 实例设置为 nil 不会产生警告或崩溃