[英]Animation completion block run prior to finish?
我將使用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,然后突然反彈回模糊。 為什么是這樣? 閃爍到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? */
});
我懷疑UIView動畫方法實際上是立即執行該塊以便確定正在更改的可設置動畫的屬性,因此您的視覺效果視圖會立即設置為nil,因為這不是可設置動畫的屬性。
您可以使用簡單的animateWithDuration
並使用dispatch_after
來延遲動畫,而不是使用animateWithDuration:animationDuration delay:...
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); }
}];
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.