开发者

Fade to black within a 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?


EDIT

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];
}
  1. Create a UIView in the navigationbar's superview (which I'm assuming is window-sized) that is the same size as the window.
  2. Set that view's backgroundColor to [UIColor blackColor], and its alpha to 0.0.
  3. In your button handler do something like the above (assuming your new UIView is blackView and ANIMATION_DURATION is your desired animation time in seconds).
  4. Then, add your new view on top.

EDIT: 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.


You can add a black coloured UIView in screen size on top of your current view, and animate its alpha from 0 to 1. When the animation is done, add your new view. You can remove the black one then. Animate from 1 to 0 for the opposite effect - going from black to the content).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜