如何淡出子视图以显示从.nib加载的UIViewController视图

时间:2012-12-06 01:14:22

标签: objective-c uiview uiviewcontroller ios6

我有一个从.nib文件加载的ViewController。在viewDidLoad方法中,我创建一个子视图并将其添加到视图层次结构中。如何淡出该子视图以显示.nib文件中的视图?

(子视图就像一个闪屏,我想淡出来显示.nib中的视图,它是这样设置的,因为它对我来说是最简单的方法。)

以下是我的一些代码(我试图在viewDidLoad中从nib设置对原始视图的引用,但无法使其工作):

[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");
}];

}

该代码崩溃了。我究竟做错了什么?

1 个答案:

答案 0 :(得分: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,那么此代码段中的内存管理是错误的。