2012-11-17 56 views
4

我正试图淡入淡出UIView中创建的动画Alpha。我需要淡入它,几秒钟后可见,然后淡出它。使用UIView animateWithDuration立即更改Alpha:而不是动画

淡出功能正常工作。视图顺利消失。但淡入只会使视图立刻显示出来,而不是在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]; 
} 

回答

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

的问题是,因为你的方法是设置动画。

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

当你直后的第一个观点实际上是说“好,我需要从动画1.0(模型中的我的电流值)0.0”,这就是为什么你没有看到你所期望的定义第二块。

+0

太棒了。非常有意义。谢谢你的回复,保罗。 – Murdock

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

这个解决方案的更好,因为执行视图控制器具有不仅完全控制的时候,却是什么鬼触发淡出(例如,用户操作)。

相关问题