UIView's animateWithDuration delay not delaying animation
Asked Answered
T

3

12

I am trying to perform an animation on a label where a flip animation happens and after it is done and after a delay, The text of the label changes.

It seems that the delay never happens. The text immediately changes after the flip completes although I am using UIView animateWithDuration:0.5 delay:4.0 in the completion block. If Instead I do a performSelector with Delay in the completion block (the commented statement) it works as expected. Any idea why the delay value is being ignored?

- (void) flipShapeWithText:(NSString *)text {

    [UIView transitionWithView:someLabel duration:0.15 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        someLabel.text = text;  
    }completion:^ (BOOL finished){
//        [self performSelector:@selector(updateLabelText:) withObject: @"New Text" afterDelay:4.0];
    [UIView animateWithDuration:0.5
                              delay:4.0
                            options: UIViewAnimationOptionTransitionCrossDissolve
                         animations:^{
                             currentShapeNameLabel.text =  @"New Text" ;}
                         completion:nil];
    }];
}
Trier answered 23/11, 2011 at 16:18 Comment(0)
C
20

The delay param of animateWithDuration:delay:options:animations:completion specifies the delay before the animation occurs. You are setting the text within the animation block so after the delay is over, the animations begin which immediately changes the text as that change is not animatable. To do what you want, change the text in the completion block as follows:

    [UIView animateWithDuration:0.5
                          delay:4.0
                        options: UIViewAnimationOptionTransitionCrossDissolve
                     animations:^{ // anything animatable }
                     completion:^(BOOL finished) {
                         currentShapeNameLabel.text =  @"New Text" ;}];

You can eliminate the delay if you want the animation to start immediately. If you want the text change to happen 4 secs after the animation completes add that delay in the completion block either with dispatch_after() or performSelector:withDelay:.

Cheviot answered 23/11, 2011 at 17:49 Comment(1)
FYI: Setting nil as the argument for the animations block is illegal -- according to the current state of Apple's documentation, at least.Thereunder
D
9

In my case, the problem was that earlier in the code I was calling UIView's snapshotViewAfterScreenUpdates with a value true. After changing that to false it worked fine.

Devote answered 3/2, 2016 at 16:14 Comment(2)
I had this same problem, and you saved me a bit of time with the debugging, so thanks :). In my case I couldn't set this value to false, so I just use dispatch_after to perform this particular animation and it worked fine.Rhodarhodamine
Thats pretty obscure. Good catch.Mcfarlane
P
-1

try nesting in

dispatch_async(dispatch_get_main_queue(), ^{
});
Potiche answered 1/9, 2019 at 12:19 Comment(1)
Try to provide more details with you answer about how it solves the OP question.Narda

© 2022 - 2024 — McMap. All rights reserved.