I have an app with multiple animations that are simple, just an image moving from offscreen to onscreen and then back offscreen. I’m using animation blocks, and the code is like this:
void (^animationOne) (void) = ^{imageOne.center = d;};
void (^afterOne) (BOOL) = ^(BOOL f) {imageOne.center = dOrig;};
[UIView animateWithDuration:.85 delay:3 options:opts animations:animationOne completion:afterOne];
There are several others that more or less follow the same format. However, I’d like the order in which they appear to be random. Part of the problem is that both the animations and completion blocks are specific to one another, and are required, so I can’t just stick the animations in an array, shuffle it, and then use the array for the animations. Is there a way for me to randomize this using the animation blocks, and if not, is there a different way I can do this so that I can randomize the order? Thanks.
Edit: Thanks to some helpful suggestions from vikingosegundo, I’ve altered my code to look like this:
NSArray *animationsCompletions = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:anim1,@"animation",after1, @"completion", nil],
[NSDictionary dictionaryWithObjectsAndKeys:anim2,@"animation",after2, @"completion", nil],
[NSDictionary dictionaryWithObjectsAndKeys:anim3,@"animation",after3, @"completion", nil],
[NSDictionary dictionaryWithObjectsAndKeys:anim4,@"animation",after4, @"completion", nil],
nil];
NSDictionary *animationCompletionPair = [animationsCompletions randomElement];
[UIView animateWithDuration:.85 delay:3 options:opts animations:[animationCompletionPair objectForKey:@"animation"] completion:[animationCompletionPair objectForKey:@"completion"]];
[UIView animateWithDuration:.85 delay:8 options:opts animations:[animationCompletionPair objectForKey:@"animation"] completion:[animationCompletionPair objectForKey:@"completion"]];
[UIView animateWithDuration:.85 delay:13 options:opts animations:[animationCompletionPair objectForKey:@"animation"] completion:[animationCompletionPair objectForKey:@"completion"]];
[UIView animateWithDuration:.85 delay:18 options:opts animations:[animationCompletionPair objectForKey:@"animation"]completion:[animationCompletionPair objectForKey:@"completion"]];
However, now I’m receiving a Program received signal: EXC_BAD_ACCESS error when the view with the animations tries to load, and no other information is given by the compiler. Any suggestions would be appreciated.
Stick the animation and completion blocks in an array (or two). Instead of shuffling the array, just pick indices at random and execute the corresponding animation blocks.
For example, using two arrays:
If you want each animation to run only once, then create a second array of indices and initialize it with numbers from 0 to n-1. Then shuffle that array and iterate over it executing the animation for the index you find at each position.