在for循环内淡化动画

时间:2014-01-02 22:03:16

标签: ios uiview

至少我会梦想这个动画流程:

  1. 淡化为50%,alpha:0.5持续2秒
  2. 然后用alpha:1淡化到100%2秒
  3. 然后使用alpha:0淡出为0%4秒
  4. 在可见动画结束后,应更改标签的文本,然后如上所示再次开始淡入淡出例程。

    我觉得这里的代码不允许逐步淡入淡出动画并且代码开始得太早而无需等待完成淡出部分。

    //A FOR Loop sets text for the _label then the Animation routine should start:
    
    - (IBAction)startFade:(id)sender
    {
        [_label setAlpha:0.f];
    
        [UIView animateWithDuration:2.f
                              delay:0.f
                            options:UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             [_label setAlpha:1.f];
                         }
    
                         completion:^(BOOL finished) {
                             [UIView animateWithDuration:2.f
                                                   delay:0.f
                                                 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^{
                                                  [_label setAlpha:0.f];
                                              }
                                              completion:nil];
                         }
         ];
    }
    
    // Somehow the code shall wait here for ending the visible (!!) animation in the UI
    // to set a new text for _label with the FOR Loop
    

    到目前为止,整个问题花了我几天才弄明白。但是由于它没有自动等待动画的视觉结束而使我很难继续下去,所以我被困在这里暂停代码一段时间了。

1 个答案:

答案 0 :(得分:0)

我确信这就是你真正想要的;但它符合你的描述:

- (IBAction)startFade:(id)sender {
    [_label setAlpha:0.f];

    [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveLinear animations:^{
        [_label setAlpha:0.5f];
    } completion:^(BOOL finished) {
        NSLog(@"1st step is done.");
        [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveLinear animations:^{
            [_label setAlpha:1.f];
        } completion:^(BOOL finished) {
            NSLog(@"2nd step is done.");
            [UIView animateWithDuration:4.f delay:0.f options:UIViewAnimationOptionCurveLinear animations:^{
                [_label setAlpha:0.f];
            } completion:^(BOOL finished) {
                NSLog(@"3rd step is done, text will be changed.");
                // let me change the text
                [_label setText:[[NSDate date] description]];
                // let me start the animation over, welcome to my infinite loop.
                [self startFade:nil];
            }];
        }];
    }];
}