如何在按下按钮之前停止响应摇动? [英] How to stop responding to a shake before a button is pressed?

查看:90
本文介绍了如何在按下按钮之前停止响应摇动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作iPhone应用程序,动画会对小动作做出反应,这是我的代码:

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?

推荐答案

只需按下按钮即可设置变量。然后在确定是否发生摇动的方法中,首先检查按钮是否已被点击。

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.

例如,在didAccelerate方法中,您可以先检查是否按钮已被按下,如果没有,只需返回。

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

这篇关于如何在按下按钮之前停止响应摇动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆