How to stop responding to a shake before a button is pressed?

╄→尐↘猪︶ㄣ 提交于 2019-12-31 07:38:06

问题


i am currently making a iPhone app, and a animation reacts to a small shake, this is my code:

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;
        }

On the same view controller I have another page before the animation, now I need them to press the button 'start' and then they can shake, but before they press 'start' if they shake, nothing would happen. How would I do this?


回答1:


Just have a variable that gets set when they press the button. Then in the method that determines whether a shake has taken place, first check whether the button has been clicked.

For example, in the didAccelerate method, you could first check if the button had been pressed, and if not, just return.



来源:https://stackoverflow.com/questions/11865161/how-to-stop-responding-to-a-shake-before-a-button-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!