2

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

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

这是我的一些代码(我试图从 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");
}];

}

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

4

1 回答 1

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,那么此代码段中的内存管理是错误的。

于 2012-12-06T07:40:09.780 回答