Alpha 用 UIView animateWithDuration 立即改变:而不是动画

Mur*_*ock 5 animation objective-c uiview

我正在尝试淡入通过动画 Alpha 创建的 UIView。我需要将其淡入,让它可见几秒钟,然后淡出。

淡出功能效果很好。视线顺利消失。但淡入只是让视图立即出现,而不是在 0.5 秒的间隔内缓慢出现。

所以看起来动画的淡入淡出不起作用,只是立即将 alpha 设置为1.0。我在这里有点不知所措。有什么想法我做错了吗?谢谢!

-(void)presentPopupPhrase:(NSString *)phrase 
                   inView:(UIView *)view 
             withDelegate:(id)delegate 
            andCompletion:(void (^)(BOOL completed))completion {

    MessagePopupView *pv = [[[MessagePopupView alloc] initWithFrame:self.frame andText:phrase] autorelease];
    pv.alpha = 0.0;
    [view addSubview:pv];

    [self fadeInMPV:pv 
       withDuration:self.fadeDuration 
           andDelay:self.fadeInDelay];

    [self fadeOutMPV:pv 
        withDuration:self.fadeDuration 
          afterDelay:self.fadeOutDelay 
      withCompletion:completion 
         andDelegate:delegate];
}

-(void)fadeInMPV:(MessagePopupView *)mpv 
    withDuration:(NSTimeInterval)duration 
        andDelay:(NSTimeInterval)delay 
{    
    [UIView animateWithDuration:duration 
                          delay:delay 
                        options:UIViewAnimationOptionCurveLinear 
                     animations:^{
                         mpv.alpha = 1.0;
                     } 
                     completion:nil];
}

-(void)fadeOutMPV:(MessagePopupView *)mpv
     withDuration:(NSTimeInterval)duration
       afterDelay:(NSTimeInterval)delay
     withCompletion:(void (^)(BOOL completed))completion
      andDelegate:(id)delegate 
{
    [UIView animateWithDuration:duration 
                          delay:delay 
                        options:UIViewAnimationOptionCurveLinear 
                     animations:^{
                         mpv.alpha = 0.0;
                     } 
                     completion:completion];
}
Run Code Online (Sandbox Code Playgroud)

编辑:

如果有帮助的话,这是我调用它的 VC 代码:

-(void)viewDidAppear:(BOOL)animated {

    CGRect phraseFrame = CGRectMake(20, 341, 280, 65);
    PopupPhraseController *phraseController = [[[PopupPhraseController alloc] initWithFrame:phraseFrame] autorelease];

    [phraseController presentPopupPhrase:@"Test Phrase" inView:self.view withDelegate:self andCompletion:^(BOOL completed){
        if (completed) {
            NSLog(@"completed");
        } else {
            NSLog(@"not completed");
        }
        NSLog(@"blocked!");
    }];

    [super viewDidAppear:animated];
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*l.s 5

您不能像这样链接动画,因为第二个动画块本质上会导致第一个动画块被取消。

您有两种将多个动画链接在一起的选项:

  1. 使用第一个动画的完成处理程序
  2. 嵌套动画但延迟第二个动画

  1. 看起来像这样

    [UIView animateWithDuration:self.fadeDuration
                          delay:self.fadeInDelay
                        options:UIViewAnimationOptionCurveLinear 
                     animations:^{
                       pv.alpha = 1.0;
                     } completion:^(BOOL finished) {
    
                       [UIView animateWithDuration:self.fadeDuration
                                             delay:self.fadeOutDelay
                                           options:UIViewAnimationOptionCurveLinear 
                                        animations:^{
                                          pv.alpha = 0.0;
    
                                        } completion:completion];
                     }];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 看起来像这样

    [UIView animateWithDuration:self.fadeDuration
                          delay:self.fadeInDelay
                        options:UIViewAnimationOptionCurveLinear 
                     animations:^{
                       pv.alpha = 1.0;
    
                       [UIView animateWithDuration:self.fadeDuration
                                             delay:self.fadeOutDelay + self.fadeDuration
                                           options:UIViewAnimationOptionCurveLinear 
                                        animations:^{
                                          pv.alpha = 0.0;
                                        } completion:completion];
    
                     } completion:nil];
    
    Run Code Online (Sandbox Code Playgroud)

问题在于您设置动画的方式。

当您在视图上设置动画属性时,视图的支持模型会立即更新。因此,当您在第一个块中设置 时,alpha = 1.0视图的支持数据模型的 alpha 为1.0,然后实际动画在另一个线程上启动以显示两个值之间的插值。

当您在第一个块之后直接定义第二个块时,视图实质上是说“好吧,我需要从1.0(模型中的当前值)到0.0”进行动画处理,这就是为什么您看不到您所期望的内容。