簡體   English   中英

動畫完成塊在完成之前運行?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM