Imagine an old fashion pinball machine (or similar) with three light bulbs on either side of the machine's top scoreboard panel. The bulbs are numbered 1-6
Id like to be able to call a method that will flash the lights on for .5 sec then off again passing in an array of numbers as the sequence in which the lights should flash
I wrote the below which does work but seems very inefficient and needlessly verbose. Im thinking that using a timer would be better but Im not sure how to make that work with my "sequence" array
Is there a better way to do this?
- (IBAction)testLights:(id)sender {
NSArray *sequence = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5],nil];
[self setLights:sequence];
}
- (void)setLights:(NSArray *)sequence {
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:0]integerValue]];
light.alpha = 0;
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
//new annimation
light.alpha = 0;
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:1]integerValue]];
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
//new annimation
light.alpha = 0;
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:2]integerValue]];
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
//new annimation
light.alpha = 0;
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:3]integerValue]];
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
//new annimation
light.alpha = 0;
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:4]integerValue]];
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
//new annimation
light.alpha = 0;
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:5]integerValue]];
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
light.alpha = 0;
}];
}];
}];
}];
}];
}];
}
This can be achieved by making the function recursive. I would suggest you pass the sequence object as an NSMutableArray.
- (void)setLights:(NSMutableArray *)sequence {
if ([sequence count] > 0)
{
UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:0]integerValue]];
[sequence removeObjectAtIndex:0];
light.alpha = 0;
[UIView animateWithDuration:0.3f
animations:^{
light.alpha = 1;
}completion:^(BOOL finished){
[self setLights:sequence];
}];
}
}
This function would have an added advantage of being able to handle a sequence of any number, instead of just 6.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.