如何在button按下之前停止响应?

我目前正在做一个iPhone应用程序,一个animation反应到一个小摇,这是我的代码:

static BOOL SJHShaking(UIAcceleration* last, UIAcceleration* current, double threshold) { double deltaX = fabs(last.x - current.x), deltaY = fabs(last.y - current.y), deltaZ = fabs(last.z - current.z); return (deltaX > threshold && deltaY > threshold) || (deltaX > threshold && deltaZ > threshold) || (deltaY > threshold && deltaZ > threshold); } - (id)initWithFrame:(CGRect)frame { if (self) { // Initialization code } return self; } -(void)awakeFromNib{ [super awakeFromNib]; [UIAccelerometer sharedAccelerometer].delegate = self; } - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *) acceleration { if (self.lastAction) { if (hasBeenShaken && SJHShaking (self.lastAction, acceleration, 0.7)) { hasBeenShaken = YES; animation.animationImages= [NSArray arrayWithObjects: [UIImage imageNamed:@"Frame01.png"], [UIImage imageNamed:@"Frame02.png"], [UIImage imageNamed:@"Frame03.png"], [UIImage imageNamed:@"Frame04.png"], [UIImage imageNamed:@"Frame05.png"], [UIImage imageNamed:@"Frame06.png"], [UIImage imageNamed:@"Frame07.png"], [UIImage imageNamed:@"Frame08.png"], [UIImage imageNamed:@"Frame09.png"], [UIImage imageNamed:@"Frame010.png"], [UIImage imageNamed:@"Frame011.png"], [UIImage imageNamed:@"Frame012.png"], [UIImage imageNamed:@"Frame013.png"], nil]; [animation setAnimationRepeatCount:1]; animation.animationDuration = 1; [animation startAnimating]; [self performSelector:@selector(didFinishAnimating) withObject:nil afterDelay:1.0]; bottle.hidden=YES; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/champagne animate.wav", [[NSBundle mainBundle] resourcePath]]]; NSError *error; audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer2.numberOfLoops = 0; audioPlayer2.volume = 1; if (audioPlayer2 == nil); else [audioPlayer2 play]; [audioPlayer3 stop]; back.hidden = YES; } else if (hasBeenShaken && !SJHShaking(self.lastAction, acceleration, 0.2)) { hasBeenShaken = NO; } } self.lastAction = acceleration; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(void)dealloc{ [UIAccelerometer sharedAccelerometer].delegate = nil; } 

在同一个视图控制器上,我有另一个页面在animation之前,现在我需要他们按下“开始”button,然后他们可以动摇,但在他们动摇之前按下“开始”,什么都不会发生。 我将如何做到这一点?

只要有一个variables,当他们按下button时被设置。 然后在确定是否发生摇晃的方法中,首先检查button是否已被点击。

例如,在didAccelerate方法中,你可以首先检查button是否被按下,如果没有,就返回。