Define your animation code in a different method and call it like this:
[self performSelector:@selector(yourAnimatingMethod) withObject:nil afterDelay:1.0];
Вопрос
I'm working on a memory game with 6 UIButtons that changes background-image when they're touched. I check if two of the taped ones are the same with booleans. I have 6 different and each set to YES in each button action.
If first BOOL not is equal to second BOOL then they should change back to their original background-image. My problem is that when the second button is clicked, directly, the two buttons changes. I want the second button's image to be seen before they change. This is my code:
- (void)buttonClicked:(id)sender
{
buttonClickedBOOL = YES;
[animation stuff....]
if (randomNumberi == 0)
{
[button setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
}
else if (randomNumberi == 1)
{
[button setBackgroundImage:[UIImage imageNamed:@"windows_logo.png"] forState:UIControlStateNormal];
}
else if (randomNumberi == 2)
{
[button setBackgroundImage:[UIImage imageNamed:@"fujitsu.png"] forState:UIControlStateNormal];
}
[UIView commitAnimations];
[self gameCheck];
}
This is where I check the conditions:
- (void)gameCheck
{
if ((buttonClickedBOOL == YES) && (button2ClickedBOOL == YES))
{
button.enabled = NO;
button2.enabled = NO;
button3.enabled = NO;
button4.enabled = NO;
button5.enabled = NO;
button6.enabled = NO;
if (randomNumberi == randomNumberi2)
{
NSLog(@"The same");
}
else
{
[animations stuff....]
[button setBackgroundImage:nil forState:UIControlStateNormal];
[UIView commitAnimations];
[animations stuff....]
[button2 setBackgroundImage:nil forState:UIControlStateNormal];
[UIView commitAnimations];
}
}
The random variables are set in viewDidLoad to int numbers 0-3 and represents which images which should be set.
Решение
Define your animation code in a different method and call it like this:
[self performSelector:@selector(yourAnimatingMethod) withObject:nil afterDelay:1.0];
Другие советы
From the UIView
class reference.
Use the method animateWithDuration:delay:options:animations:completion:
This method takes a delay parameter. It also uses blocks for the animation and completion handler. It is very useful.