2012-12-06 43 views
2

我有從.nib文件加載的ViewController。在viewDidLoad方法中,我創建了一個子視圖並將其添加到視圖層次結構中。如何淡出該子視圖以在.nib文件中顯示視圖?如何淡出子視圖以顯示從.nib加載的UIViewController視圖

(子視圖就像一個閃屏,我想淡出,以顯示在筆尖的看法,它的成立這種方式,因爲對我來說是最簡單的方法。)

下面是一些我的代碼(我試圖設置爲從viewDidLoad中筆尖原始視圖的參考,但不能得到它的工作):

[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
NSLog(@"View did load"); 
//set reference to view in .nib here 
UIView *currentView = self.view; 

CGRect frame = [[UIScreen mainScreen] bounds]; 
splashView = [[splashView alloc] initWithFrame:frame]; 
[[self view] addSubview:splashView]; 
//transition did not work 

[UIView transitionFromView:splashView toView:currentView duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) { 
    NSLog(@"transition finished"); 
}]; 

} 

該代碼崩潰。我究竟做錯了什麼?

+0

你看過使用storyboard/segues嗎? –

回答

1

嘗試代替原來的代碼如下:

[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
NSLog(@"View did load"); 

CGRect frame = [[UIScreen mainScreen] bounds]; 
splashView = [[splashView alloc] initWithFrame:frame]; 
[[self view] addSubview:splashView]; 
[self.view bringSubviewToFront: splashView]; 

[UIView animateWithDuration: 2.0 
        delyay: 0.5 // omit if you don't need a delay 
        options: UIViewAnimationCurveEaseOut // check the documentation for other options 
       animations: ^{ 
        splashView.alpha = 0; 
       } 
       completion: ^(BOOL finished) { 
        [splashView removeFromSuperView]; 
       }]; 

我不知道,如果你正在使用ARC與否,或者如果你使用故事板還是不行! 如果你使用ARC註釋,那麼這段代碼中的內存管理是錯誤的。

+0

我沒有使用故事板,因爲我讀過的一些文獻說故事板雖然易於使用,卻傾向於混淆初學者學習一些核心概念。我正在使用iOS 6和ARC。 非常感謝Nenad M的工作。片段中的內存管理錯誤如何? –

+0

嗨!如果你使用ARC,那麼你可以使用提供的示例。在非ARC代碼中,你必須在某個時間點釋放/自動釋放splashView。 –

+0

聽起來不錯。謝謝你的幫助。 –

相關問題