Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 567
user1114881 Avatar asked Nov 30 '22 11:11

user1114881


2 Answers

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]];
like image 92
Mutawe Avatar answered Dec 24 '22 18:12

Mutawe


Or you can code it at viewWillAppear.

- (void)viewWillAppear:(BOOL)animated {
    [UIView animateWithDuration:1.0
                          delay:2.0
                        options:UIViewAnimationOptionTransitionCrossDissolve
                     animations:^{
                         //yourAnimation
                     } completion:^(BOOL finished){
                         NSLog(@"Animation is finished");
                     }];
}
like image 30
Penguin Avatar answered Dec 24 '22 18:12

Penguin