在uinavigationcontroller中变黑了吗? [英] Fade to black within a uinavigationcontroller?
问题描述
当用户在显示新视图之前按下uinavigationcontroller上的按钮时,我想将整个屏幕(包括导航栏)淡化为黑色. (出于各种原因,我不想推这个新观点). 我将如何实现呢?
I want to fade the whole screen (including navigation bar) to black when a user presses a button on a uinavigationcontroler, before showing a new view. (i don't want to push this new view, for various reasons). How would I achieve this?
编辑
感谢Mac和Eiko,我已经弄清楚了.这是我使用的代码.不确定它是否是最佳选择,但可以解决问题.
Thanks to Mac and Eiko, I have figured it out. Here's the code I used. Not sure if it is optimal, but it does the trick.
// this is called from a programmatically constructed button.
// change (void) to (IBAction) if linking from IB.
- (void)fadeAndShow:(id)sender
{
// blackView is a @property which has been @synthesize-d
// do I really need to alloc and init this?
blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.alpha = 0.0;
[blackView setBackgroundColor:[UIColor blackColor]];
[self.navigationController.navigationBar.superview addSubview:blackView];
[UIView beginAnimations:@"fadeAway" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDidStopSelector:@selector(showNewScreen:finished:context:)];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
-(void)showNewScreen:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
// I guess you could fade in somewhere in the new view controller.
// don't know how to fade back in this view tho... viewDidAppear?
NewViewController *controller = [[NewViewController alloc] initWithNibName:@"NewView" bundle:nil];
[self.navigationController setNavigationBarHidden:YES];
controller.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:controller animated:NO];
[blackView removeFromSuperview];
[controller release];
}
推荐答案
离我远去(我实际上还没有测试以下内容):
Off the top of my head (I haven't actually tested the following at all):
-(IBAction) buttonClicked:(id) sender
{
[UIView beginAnimations:@"myAnimation" context:nil];
[UIView setAnimationDuration:ANIMATION_DURATION];
blackView.alpha = 1.0;
[UIView commitAnimations];
}
- 在导航栏的超级视图中创建一个
UIView
(我假设它是窗口大小的),该大小与窗口的大小相同. - 将该视图的
backgroundColor
设置为[UIColor blackColor]
,并将其alpha
设置为0.0
. - 在按钮处理程序中执行上述操作(假设新的
UIView
是blackView
,而ANIMATION_DURATION
是所需的动画时间,以秒为单位). - 然后,在顶部添加新视图.
- Create a
UIView
in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window. - Set that view's
backgroundColor
to[UIColor blackColor]
, and itsalpha
to0.0
. - In your button handler do something like the above (assuming your new
UIView
isblackView
andANIMATION_DURATION
is your desired animation time in seconds). - Then, add your new view on top.
对我来说太快了Eiko!此外,由于排序列表似乎与代码格式有关,因此代码位于顶部,很抱歉,答案看起来有些奇怪.
too quick for me Eiko! Also, code at the top since the ordered list seems to screw around with the code formatting - sorry the answer reads a little odd.
这篇关于在uinavigationcontroller中变黑了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!