Why do some animatable properties set outside an a

2019-07-28 01:13发布

问题:

I have a bit of code that goes like this:

//up till now someButton's alpha was 1
someButton.alpha = 0;
[UIView animateWithDuration:.25
                      delay:0.0
                    options:kMaskEaseOut
                 animations:^ {
                     someButton.alpha = 1;
                 }
                 completion:^ (BOOL finished){}];

The problem is someButton's alpha isn't set to 0 before the animation begins, ie nothing visually happens. Now, if I comment out the entire animation block it will indeed set the alpha of someButton to 0. Also, if I do this:

    [UIView animateWithDuration:0
                      delay:0.0
                    options:kMaskEaseOut
                 animations:^ {
                     someButton.alpha = 0;
                 } completion:^ (BOOL finished){
                     [UIView animateWithDuration:.25
                                           delay:0.0
                                         options:kMaskEaseOut
                                      animations:^ {
                                          someButton.alpha = 1;
                                      }
                                      completion:^ (BOOL finished){}];
                 }];

it works fine (I start the animation after a 0 length animation) which is kind of silly.

回答1:

Well, may be because the duration it takes to set the alpha to zero is so low that you cannot see it (It's just a line of code - happens momentarily - and executes as fast as any other line of code), but from the moment on, it takes .25 seconds to change the alpha back to 1. That's probably the reason you don't see the animation of alpha setting to 0, but can see it going back to 1. The same explanation holds good for your second code sample.



回答2:

Check this, specially the section Animations(they have a similar example than yours: hideShowView). The reason of this difference of behavior in your two codes is that animation are occuring in another thread that takes place immediately.

 //up till now someButton's alpha was 1
 someButton.alpha = 0;
 [UIView animateWithDuration:.25
                          delay:0.0
                        options:kMaskEaseOut
                     animations:^ {
                         someButton.alpha = 1;
                     }
                     completion:^ (BOOL finished){}];
 NSLog(@"%d", someButton.alpha); // will display 1 not 0

I think you could slightly delay your animation if you don't want to comment out your animation (the animation with 0 delay in your second code source).