Problems animating UIView alpha

后端 未结 6 1972
野趣味
野趣味 2020-12-06 19:17

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.vie         


        
相关标签:
6条回答
  • 2020-12-06 19:37

    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();
                        }
                    }
                );
    
    0 讨论(0)
  • 2020-12-06 19:42

    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.

    0 讨论(0)
  • 2020-12-06 19:43

    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.

    0 讨论(0)
  • 2020-12-06 19:47

    One more piece of the puzzle. When you're animating constraints, you can set the new constraint constants outside of an animation block and then call layoutIfNeeded inside the animation.

    With view alphas, these need to be set directly inside the animation block.

    Change this:

    //setup position changes
    myConstraint.constant = 10
    myOtherConstraint.constant = 10
    //whoops, alpha animates immediately
    view.alpha = 0.5
    
    UIView.animate(withDuration: 0.25) {
       //views positions are animating, but alpha is not   
       self.view.layoutIfNeeded()
    }
    

    to

    //setup position changes
    myConstraint.constant = 10
    myOtherConstraint.constant = 10
    
    UIView.animate(withDuration: 0.25) {
       view.alpha = 0.5
       self.view.layoutIfNeeded()
    }
    
    0 讨论(0)
  • 2020-12-06 19:52
    [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.

    0 讨论(0)
  • 2020-12-06 19:57

    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) {
        }];
    });
    
    0 讨论(0)
提交回复
热议问题