2012-11-17 57 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]; 
}); 

這個解決方案的更好,因爲執行視圖控制器具有不僅完全控制的時候,卻是什麼鬼觸發淡出(例如,用戶操作)。

相關問題