iphonexcode5animatewithduration

using UIView animateWithDuration: delay: Xcode


I am trying to use

[UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^ (BOOL completed) {}         ];

however no matter what number I put for the delay, there is no delay.
I am trying to have a series of buttons fade in one after the other when another button is touched. So I use a series of UIView animate with various lengths of delay but they all show up at the same time no matter what time delay I enter. Does anyone have some suggestions?


Solution

  • First of all, the values animateWithDurationand delay are float values, and should be as 1.0 and 2.0

    Opinion: if your code is at the ViewDidLoad Method, try to move it to a separated function

    Secondly if you are not able to do it by this context, jump to another, or make a walk around such as using

    performSelector:withObject:afterDelay
    

    Another walk around is to set the delay to 0, and place the subsequent UIView animation blocks in the completion block of the previous animateWithDuration statements

     [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^(void) {
            //Leave it empty
                     }
                     completion:^(BOOL finished){
    
    // Your code goes here
       [UIView animateWithDuration:1.0 delay:0.0 options:
       UIViewAnimationOptionCurveEaseIn animations:^{
            } completion:^ (BOOL completed) {}];
        }];
    

    PS: some properties are not supported to be animated such as 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.

    in the text animation case, To do what you want, change the text in the completion block as follows:

    [UIView animateWithDuration:1.0
                          delay:2.0
                        options: UIViewAnimationOptionTransitionCrossDissolve
                     animations:nil
                     completion:^{
                         lbl.text =  @"My text" ;
    }];
    

    Also backgroundColor is NOT animated in UIView. Instead, use backgroundColor property of the view's layer:

    [[controller layer] setBackgroundColor:[[UIColor anyColor] CGColor]];