Fadeout transition in modal view
i have created a transparent modal view and it works fine. What i wanted is to make a fade in transition to take place when the modal view appears..below is the code..
UIView *modalView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
modalView.opaque = NO;
modalView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.9];
UILabel *label1 =开发者_运维百科 [[[UILabel alloc] init] autorelease];
label1.text = @"Modal View";
label1.textColor = [UIColor whiteColor];
label1.backgroundColor = [UIColor clearColor];
label1.opaque = NO;
[label1 sizeToFit];
[modalView addSubview:label1];
[self.view addSubview:modalView];
Use the following code to fade in.
// Fade In
- (void)fadeInModalView {
[self.view addSubview:modalView];
modalView.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
modalView.alpha = 0.9;
[UIView commitAnimations];
}
To fade out.
// Fade Out
- (void)fadeOutModalView {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(removeModalView)];
modalView.alpha = 0.0;
[UIView commitAnimations];
}
To remove modalView after it faded out.
// Remove modalView after it faded out
- (void)removeModalView {
[modalView removeFromSuperview];
}
精彩评论