kingston
kingston

Reputation: 1553

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];

Upvotes: 0

Views: 1158

Answers (1)

EmptyStack
EmptyStack

Reputation: 51374

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];
}

Upvotes: 3

Related Questions