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

时间:2012-11-17 00:25:52

标签: objective-c animation 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];
}

修改

如果有帮助,这里是我从以下地方调用它的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];
}

2 个答案:

答案 0 :(得分:4)

您无法将动画链接起来,因为第二个动画块实际上会导致第一个动画块被取消。

Linking Multiple Animations Together有两种选择:

  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];
                       }];
      
    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];
      

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

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

      当您在第一个视图之后直接定义第二个块时,视图基本上表示“确定我需要从1.0(我在模型中的当前值)设置动画到0.0”,这就是为什么你要看不出你的期望。

答案 1 :(得分:-1)

感谢Paul.s放弃那里的暗示。我认为这种预期的行为有点奇怪,即使从低层次来看似乎合乎逻辑。

为了扩展Paul的2个解决方案,有一个第三个解决方案是更好的IMO。

使用GCD为淡出代码添加延迟,例如:

//Make sure this delay exceeds your fade-in time...
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self fadeOutMPV:pv
        withDuration:self.fadeDuration
          afterDelay:self.fadeOutDelay
      withCompletion:completion
         andDelegate:delegate];
});

这个解决方案更好,因为执行视图控制器不仅可以完全控制何时触发淡出(例如,用户操作)。

相关问题