using UIView animateWithDuration: delay: Xcode

☆樱花仙子☆ 提交于 2019-12-03 10:17:15

问题


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?


回答1:


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]];



回答2:


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");
                     }];
}



回答3:


Before you implement please read it duration: The total duration of the animations, measured in seconds. If you specify a negative value or 0, the changes are made without animating them. delay : The amount of time (measured in seconds) to wait before beginning the animations. Specify a value of 0 to begin the animations immediately. options

-Use float values for both of above parameters - It would be ice if you define your animation operation in separate routine &

call it appropriate location like in ViewDidAppear/viewWillapear.

Use function like this & set your UI Element properties like this:
 [UIView animateWithDuration:0.6
                          delay:0.3
                        options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [self.view layoutIfNeeded];
                         [yourviews setTintColor:[UIColor whiteColor]];
                         self.view.backgroundColor = [UIColor greenColor];
                     }
                     completion:^(BOOL finished){

                         // You next action to perform after completion;
                     }];
}

Happy coding , hope it will help you.



来源:https://stackoverflow.com/questions/21547241/using-uiview-animatewithduration-delay-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!