Reputation: 509
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.
Upvotes: 2
Views: 464
Reputation: 52227
Caleb's answer is right, but you should consider of using a category method on NSArray to get a random element, just to avoid code repetition and re-use tested code.
A [array randomElement];
could be like this:
-(id)randomElement
{
if ([self count] < 1) return nil;
NSUInteger randomIndex = arc4random() % [self count];
return [self objectAtIndex:randomIndex];
}
You will find this implementation and other convenient tools on NSArray in my arraytools.
In this case the array would be filled with both, animations and completions block as pairs.
NSArray *animationsCompletions = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:animation1,@"animation",completion1, @"completion", nil],
[NSDictionary dictionaryWithObjectsAndKeys:animation2,@"animation",completion2, @"completion", nil],
[NSDictionary dictionaryWithObjectsAndKeys:animation3,@"animation",completion3, @"completion", nil],
nil];
NSDictionary *animationCompletionPair = [animationsCompletions randomElement];
[UIView animateWithDuration:.85
delay:3
options:opts
animation:[animationCompletionPair objectForKey:@"animation"]
completion:[animationCompletionPair objectForKey:@"completion"]];
Upvotes: 1
Reputation: 124997
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:
NSArray *animations = [NSArray arrayWithObjects:animation1, animation2, animation3, nil];
NSArray *completions = [NSArray arrayWithObjects:completion1, completion2, completion3, nil];
int i = arc4random() % 3;
[UIView animateWithDuration:.85
delay:3
options:opts
animation:[animations objectAtIndex:i]
completion:[completions objectAtIndex:i]];
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.
Upvotes: 3