2012-06-28 91 views
1

我對Objective-C和iOS開發一般都比較陌生,希望對我的問題有所幫助和指導。從[email protected]過渡到MainView

我寫了我的第一個應用程序,這是一個具有主視圖和翻轉視圖的「實用」應用程序。該應用程序可以在模擬器和iPhone上按預期的方式工作。然而,從Default.png圖像到我的主視圖有一個明顯而突然的變化。

我跟着n13的代碼https://stackoverflow.com/a/9918650/1324822,但是,我得到的只是主視圖,接着是淡入黑色。

我的代碼,內AppDelegate.m文件如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

// set up your root view and stuff.... 
//.....(do whatever else you need to do)... 

// show the main window, overlay with splash screen + alpha dissolve... 
UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]]; 
[self.window addSubview:splashScreen];   
[self.window makeKeyAndVisible]; 

[UIView animateWithDuration:0.3 animations:^{splashScreen.alpha = 0.0;} 
       completion:(void (^)(BOOL)) ^{ 
        [splashScreen removeFromSuperview]; 
       } 
]; 
return YES; 
} 

我承認,我是「蒙冤」我的項目的一部分,雖然我非常渴望瞭解是什麼原因造成的問題,以及如何解決它。我希望我需要指定我的主視圖上面的某處,也許將它初始化爲註釋,但我不確定如何做到這一點,因爲沒有過渡,唯一的要求是設置return YES;

如果有幫助,這是一個故事板項目。我正在運行Xcode 4.3.3。

再次感謝。

+0

如果您正在使用故事板,爲什麼不使用拖放式繼續?如果你是新手Objective-C,我建議你儘可能少編寫代碼。 – Dustin

+0

這是否允許我創建上面的轉換淡入淡出?我確實想寫代碼,但更重要的是理解它。 –

+0

是的,你可以設置轉換的類型 – Dustin

回答

2

這就是我要做的閃屏:

我不把它進入App代表,而不是被視爲第一個屏幕上。所有你需要的是你的rootViewController中的兩個實例變量,第一個是UIView *黑色,第二個是UIImageView * splash。然後:

-(void) viewWillAppear:(BOOL)animated { 

static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 

    self.view.userInteractionEnabled = NO; 
    black = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    black.backgroundColor = [UIColor blackColor]; 


    splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    [splash addSubview:indicator]; 
    indicator.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, 455); 

    [indicator startAnimating]; 
    //the indicator part is arbitrary, of course 

    splash.image = [UIImage imageNamed:@"Default.png"]; 

    [self.view addSubview:black]; 
    [self.view addSubview:splash]; 

}); 


} 

然後:

-(void) viewDidAppear:(BOOL)animated { 

static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 



    [UIView animateWithDuration:0.4 delay:2 options:UIViewAnimationCurveEaseInOut animations:^{ 
     splash.alpha=0; 
    } completion:^(BOOL finished) { 
     [splash removeFromSuperview]; 

     [UIView animateWithDuration:0.45 delay:0.2 options:UIViewAnimationCurveEaseInOut animations:^{ 
      black.alpha = 0; 
     } completion:^(BOOL finished) { 
      [black removeFromSuperview]; 
      black = nil; 
      splash = nil; 
      self.view.userInteractionEnabled = YES; 

     }]; 
    }]; 

}); 


} 

我沒有測試此代碼與具有導航控制器或標籤視圖控制器的應用程序,但它的工作原理是與普通視圖控制器魅力。

+0

謝謝,今晚我會試試這個! –

相關問題