How to let an animation finish before the next userinput is allowed
I have some UIImageViews and when the user is touching, an animation with a certain duration starts. A trigger counts ++ and with the next touch another animation starts to play. But if the user touches to fast or make a doubletouch the first animation does not finish until the last frame. I tried the "sleep()" command, but it does not work.
#pragma mark HenneAnimation
if([touch view] == ani_Henne){
//trigger strats with zero
switch (trigHenne) {
case 0:
//firstanimation 1 sec
ani_Henne.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"ani_Henne01.png"],
[UIImage imageNamed:@"ani_Henne01.png"],
[UIImage imageNamed:@"ani_Henne02.png"],
[UIImage imageNamed:@"ani_Henne01.png"],
[UIImage imageNamed:@"ani_Henne01.png"],nil];
ani_Henne.animationDuration = 1;
ani_Henne.animationRepeatCount = 1;
[ani_Henne startAnimating];
[self.view addSubview:ani_Henne];
trigHenne++;
break;
case 1:
//second animation 1 sec
ani_Henne.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"ani_Henne03.png"],
[UIImage imageNamed:@"ani_Henne05.png"],
[UIImage imageNamed:@"ani_Henne03.png"],
[UIImage imageNamed:@"ani_Henne03.png"],
[UIImage imageNamed:@"ani_Henne04.png"],
[UIImage imageNamed:@"ani_Henne05.png"],
[UIImage imageNamed:@"ani_Henne03.png"],nil];
ani_Henne.animationDuration = 3;
ani_Henne.animationRepeatCount = 1;
[ani_Henne startAnimating];
[self.view addSubview:ani_Henne];
trigHenne++;
break;
case 2:
[self.view bringSubviewToFront:ani_Henne];
ani_Henne.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"ani_Henne06.png"],
[UIImage imageNamed:@"ani_Henne07.png"],
[UIImage imageNamed:@"ani_Henne06.png"],
开发者_StackOverflow中文版 [UIImage imageNamed:@"ani_Henne06.png"],
[UIImage imageNamed:@"ani_Henne08.png"],
[UIImage imageNamed:@"ani_Henne08.png"],
[UIImage imageNamed:@"ani_Henne09.png"],
[UIImage imageNamed:@"ani_Henne08.png"],
[UIImage imageNamed:@"ani_Henne09.png"],
[UIImage imageNamed:@"ani_Henne08.png"],
[UIImage imageNamed:@"ani_Henne07.png"],
[UIImage imageNamed:@"ani_Henne08.png"],
[UIImage imageNamed:@"ani_Henne09.png"],
[UIImage imageNamed:@"ani_Henne08.png"],nil];
ani_Henne.animationDuration = 2.75;
ani_Henne.animationRepeatCount = 1;
[ani_Henne startAnimating];
[self.view addSubview:ani_Henne];
trigHenne++;
break;
case 3:
trigHenne=0;
// etc. animations
break;
default:
break;
}
}
When you begin to animate set the userinteraction disabled, and when you are done, reenable it like:
yourImageView.userInteractionEnabled = NO;
yourImageView.userInteractionEnabled = YES;
you could also delay the enabling with
...
[self performSelector:@selector(enable) withObject:yourImageView afterDelay:1.0];
...
-(void)enable
{
yourImageView.userInteractionEnabled = YES
}
Using UIApplications beginIgnoringInteractionEvents and endIgnoringInteractionEvents might help you, when begin is called UI interaction events from user are ignored, here is a reference UIApplication ref
精彩评论