Delaying between animations in Objective C
-
12-12-2019 - |
Question
I'm trying to delay between turning each button's alpha to 0.0, but what I have is not working.
[UIView animateWithDuration:1.5 delay:.25 options:UIViewAnimationCurveLinear animations:^{button.alpha = 0.0;
button2.alpha = 0.0;
button3.alpha = 0.0;
button4.alpha = 0.0;
button5.alpha = 0.0;} completion:^(BOOL finished){ ; }];
How does one animate with a delay?
Solution
The delay is to start the animation, not a delay between each change in the animation block.
You've made things unnecessarily difficult on yourself by having a sequence of buttons and deciding to create individual variables manually instead of using an array. If you've got a collection of variables, you should usually store them in a collection of some sort.
Here's one way you could do it if they were in an array:
NSUInteger i = 0;
float duration = 1.5f;
for (UIButton *button in buttons) {
[UIView animateWithDuration:duration delay:0.25f+duration*i UIViewAnimationCurveLinear animations:^{
button.alpha = 0.0f;
} completion:NULL];
++i;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow