It seemed as though the animations would not run inside the for loop for each individual button. I tried to wrap the whole FOR loop in a beginAnimations call, but that didn't work either.
I was finally able to animate the buttons by setting all the dynamically created buttons to be hidden at first in the FOR loop and then just outside the FOR loop I created a timer that would unhide them while doing the animation.
Here's the code changes I made:
Code:
UIImage *buttonBackground = [UIImage imageNamed:@"ButtonRed.png"];
for (NSInteger x = 0; x < 4; x++) {
CGRect btnFrame = CGRectMake(x * (buttonBackground.size.width+2),
y * (buttonBackground.size.height + 1),
buttonBackground.size.width,
buttonBackground.size.height);
UIButton *gridButton = [[[UIButton alloc] initWithFrame: btnFrame] retain];
[gridButton setBackgroundImage:buttonBackground forState:UIControlStateNormal];
[buttonBackground release];
// ****** I took out all animation code ******
[self.view addSubview:gridButton];
gridButton.hidden = YES; // <----------- ****** new code ******
[gridButton release];
}
// **** the following code was added just outside the FOR loop to create timer that will animate buttons as they're un-hidden.
// NSTimer gridTimer is created in the header file.
gridTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(showGrid) userInfo:nil repeats:NO];
Here's the new method I call with the timer:
Code:
-(void) showGrid{
for (UIButton *obj in self.view.subviews) {
if ([obj isMemberOfClass:[UIButton class]]) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:obj cache:YES];//~~self.view cache:YES];
obj.hidden = NO;
[UIView commitAnimations];
}
}
[gridTimer invalidate];
}