2012-02-22 25 views
8

thể trùng lặp:
Show activity indicator during application launchThêm một Hoạt động Chỉ số lập trình để một Xem

All,

Bên đại biểu ứng dụng của tôi, tôi tạo ra một cái nhìn giật gân hoạt hình có sử dụng mặc định của tôi .png. Đó là tất cả các công trình OK nhưng tôi không thể hiểu làm thế nào có được ActivityIndicator của tôi để hiển thị trên đầu trang của xem giật gân. Nó chỉ ẩn bởi khung nhìn giật gân. Dưới đây là những gì tôi có và cảm ơn:

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

Trả lời

22

Đối với tất cả những người cần thiết này, và tôi biết có rất nhiều ...
(Made trên Xcode phiên bản 4.2.1)

... Vì vậy,

Trong AppDelegate.h thêm này:

@property (nonatomic, strong) UIImageView *splashView; 

Trong AppDelegate.m thêm điều này:

Trên đầu trang của cours @synthesize splashView;
Và sau đó:

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

Các [UIView setAnimationDelay: 2,5] có trách nhiệm bao lâu splashView sẽ ở phía trước bởi thời gian trễ mà bạn chọn.

Bạn có thể thay đổi vị trí của chỉ báo bằng cách thay đổi nubmers của x/y trong:
activityIndicator.center = CGPointMake (160, 360);

Cuối cùng, dưới sự tole:

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

Chỉ cần thêm này:

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

Và có bạn đi :)
Hy vọng nó giúp.

Có một chương trình tốt đẹp ....

+0

Hi những gì nên các tham số trong (void) startupAnimationDone: (NSString *) animationID hoàn thành: (NSNumber *) hoàn thành bối cảnh: (void *) bối cảnh – morroko

+0

hidesWhenStopped = NO là chìa khóa cho tôi, nếu không nó sẽ bị ẩn đi bất kể cái gì – noobular

0

Dường như bạn ẩn (alpha -> 0,0) splashView của bạn trong 0,5 giây. Những gì bạn sẽ thấy trong trường hợp này?

+0

Đúng, điều đó chỉ mang lại cho tôi sự chuyển đổi tốt đẹp từ splashView sang chế độ xem chính. Khi tôi nhận xét nó ra, tôi vẫn nhận được kết quả tương tự - ActivityIndicator được ẩn đằng sau splashView – Slinky

0

Các bạn đã thử:

[splashView bringSubviewToFront:activityIndicator]; 
+0

Yep. Nhận kết quả tương tự. Vấn đề có thể là splashView là UIImageView. Tôi nghe nói rằng đôi khi đặt subViews trên đầu trang của UIImageViews không hoạt động – Slinky