Problems animating UIView alpha

I’m trying to apply a fade to an UIView I created programmatically on the top of another.

[UIView animateWithDuration:0.5 animations:^(void) {
    [self.view setAlpha:0];
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

The finished event is called properly after exactly 0.5 seconds, but I don’t see any fade (I should see the UIView on the bottom).

  • CoreBluetooth: How to design code for many characteristics (30 - 40)?
  • Stable way to embed open-source code into static library in iOS?
  • iOS : Objective-C : Firebase : Is it possible to read the cached data first for any reference?
  • Apple watch simulator issue
  • How to reduce iOS AVPlayer start delay
  • Simple MVC setup / design?
  • If instead of using the alpha, I move away the UIView it works (I see the bottom UIView while the top UIView slides away), so it seems to be a problem related to alpha, but I can’t figure out what’s wrong!

    [UIView animateWithDuration:0.5 animations:^(void) {
        CGRect o = self.view.frame;
        o.origin.x = 320;
        self.view.frame = o;
    }
                     completion:^(BOOL finished){
                         [self.view removeFromSuperview];
                     }];
    

    I used alpha animations previously and they works in this way usually…

    5 Solutions Collect From Internet About “Problems animating UIView alpha”

    Try setting opaque to NO explicitly. I had the same problem and setting that solved my problem. Apparently, opaque views just don’t seem to play well with alpha.

    Credit goes to Hampus Nilsson’s comment though.

                [UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    [self.view setAlpha:0];
                } completion:^(BOOL finished) {
                    [self.view removeFromSuperview];
                }];
    

    this will work right, and your fading will be nicer, because of options:UIViewAnimationOptionCurveEaseInOut

    I ran into the exact problem. In my case, I wanted the view to be hidden at first, so I set hidden to true. I thought changing the alpha value changes hidden property automatically, but that wasn’t the case.

    So, if you want the view to be hidden at first, set its alpha to 0.

    I had exactly the same problem, and none of the suggestions worked for me. I overcame the problem by using the layer opacity instead.
    This is the code for showing the layer (using Xamarin, but you’ll get the idea):

            //Enter the view fading
            zoomView.Layer.Opacity = 0;
    
            UIApplication.SharedApplication.KeyWindow.RootViewController.View.Add(zoomView);
    
            UIView.AnimateNotify(
                0.15, // duration
                () => { zoomView.Layer.Opacity = 1.0f },
                (finished) => { }
            );
    

    And this is for fading out the same zoomView

    UIView.AnimateNotify(
                    0.15, // duration
                    () => { zoomView.Layer.Opacity = 0; },
                    (finished) =>
                    {
                        if (finished)
                        {
                            zoomView.RemoveFromSuperview();
                        }
                    }
                );
    

    I experience same issue in my case its because of thread
    So i end up with write animation block in main thread.

    dispatch_async(dispatch_get_main_queue(), ^{        
        [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            View.alpha = 0.0f;
        } completion:^(BOOL finished) {
        }];
    });