2016-04-02 106 views
0

爲了執行動畫,我使用UIModalPresentationOverCurrentContext以模態方式顯示UIViewController動畫完成塊在完成之前運行?

[self presentViewController:messageVC animated:NO completion:^{ 
[messageVC displayMessageAutoReversed:YES withBlock:^(BOOL finished) { 
    if (finished) { 
     [messageVC dismissViewControllerAnimated:YES completion:nil]; 
    } 
}]; 
}]; 

messageVC,這種方法被稱爲:

-(void)displayMessageAutoReversed:(BOOL)autoReversed withBlock:(void (^)(BOOL finished))handler { 
    NSTimeInterval animationDuration = 0.4; 

    [UIView animateWithDuration:animationDuration delay:0 usingSpringWithDamping:1.5 initialSpringVelocity:2.5f options:UIViewAnimationOptionTransitionNone animations:^{ 

     self.visualEffectView.effect = [UIBlurEffect effectWithStyle:self.blurEffectStyle]; 
     self.messageLabel.alpha = 1.0f; 
     self.imageView.alpha = 1.0f; 

    }completion:^(BOOL finished) { 
     if (finished) 
     { 
      if (autoReversed) 
      { 
       [self hideMessageWithBlock:^(BOOL finished) { 
        if (handler) { handler(finished); } 
       }]; 
      } else 
      { 
       if (handler) { handler(finished); } 
      } 
     } 
    }]; 
} 

-(void)hideMessageWithBlock:(void (^)(BOOL finished))handler { 
    NSTimeInterval animationDuration = 0.4; 

    [UIView animateWithDuration:animationDuration delay:animationDuration + 1.5 usingSpringWithDamping:1.5 initialSpringVelocity:2.5f options:UIViewAnimationOptionTransitionNone animations:^{ 

     self.visualEffectView.effect = nil; 
     self.messageLabel.alpha = 0.0f; 
     self.imageView.alpha = 0.0f; 

    }completion:^(BOOL finished) { 
     if (handler) { handler(finished); } 
    }]; 
} 

但裏面hideMessageWithBlock動畫塊被立即調用,而不是1.9秒延遲後 - 它突然反彈之前設置的效果爲零回到模糊。 這是爲什麼?它有點閃爍到nil,然後跳回到模糊狀態,然後再次正確消失。

編輯:

double delayInSeconds = 2.0; 
dispatch_time_t reverseTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
dispatch_after(reverseTime, dispatch_get_main_queue(), ^(void) { 
    /* put whole animation block here? */ 
}); 
+0

我不確定'visualEffectView'動畫可以使用春天的動畫。 – Sulthan

+0

@sulthan嗯,奇怪。考慮到它的動畫效果不錯,除了閃爍之外 – Erik

+0

@ Paulw11看起來似乎是合理的,但是如何通過設置nil來淡化/動畫模糊以使其完全透明(如果它不具有動畫效果)? - 事實上有動畫,閃爍後只有一兩秒 – Erik

回答

0

我懷疑是UIView的動畫方法實際上,以確定正在發生變化的動畫性能立即執行塊,因此您的視覺效果圖被設置爲nil立即,因爲這不是一個可動畫的財產。

比使用animateWithDuration:animationDuration delay:...相反,你可以使用一個簡單animateWithDuration和使用dispatch_after延遲動畫。

double delayInSeconds = 1.9; 
dispatch_time_t reverseTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
dispatch_after(reverseTime, dispatch_get_main_queue(), ^(void) { 
    [UIView animateWithDuration:animationDuration delay:0 usingSpringWithDamping:1.5 initialSpringVelocity:2.5f options:UIViewAnimationOptionTransitionNone animations:^{ 

     self.visualEffectView.effect = nil; 
     self.messageLabel.alpha = 0.0f; 
     self.imageView.alpha = 0.0f; 

    }completion:^(BOOL finished) { 
     if (handler) { handler(finished); } 
    }]; 
});