Jon A
Jon A

Reputation: 372

Animation of alpha on UIView not working

I'm making a custom segue that fades to black, swaps out the view controller and then fades back to transparent. Unfortunately, I can't seem to get it to do that. Here's the code:

- (void)perform {

    UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [self.sourceViewController navigationController].view.frame.size.width, [self.sourceViewController navigationController].view.frame.size.height)];
    [overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.0]];
    [[self.sourceViewController navigationController].view addSubview:overlay];

    [UIView animateWithDuration:2.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
      [overlay setAlpha:1.0];
    }completion:^(BOOL finished) {
      [[self.sourceViewController navigationController] pushViewController:[self destinationViewController] animated:NO];
      [UIView animateWithDuration:2.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
        [overlay setAlpha:0.0];
      }completion:^(BOOL finished) {
        [overlay removeFromSuperview];
        NSLog(@"Done!");
      }];
    }];

}

So, what I've done is add a UIView to the navigation controller that has a black background and an alpha of 0. Then, I do an animation to change the alpha to 1, push the destination view controller onto the navigation controller, and then animate back to clear. When I do this, nothing happens. It just drops the destination view controller onto the navigation controller without any kind of animation.

Oddly, if I start with an alpha of 1 and animate to 0 and then back to 1, it works (but it isn't what I want).

I'm thinking that there's some optimization happening that excludes my clear UIView and so changing the transparency is meaningless.

Upvotes: 3

Views: 2919

Answers (1)

Marcus Adams
Marcus Adams

Reputation: 53830

What happens if you do this?

...
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]];
[overlay setAlpha:0.0];
[[self.sourceViewController navigationController].view addSubview:overlay];
...

Upvotes: 2

Related Questions