0

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?

user1483652
  • 799
  • 1
  • 9
  • 16

1 Answers1

0

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.

Liron
  • 2,012
  • 19
  • 39
  • Hey could you help it bit more, because I tried making a BOOL buttonIsPressed and i set it to yes in the ibaction Done, but when i try to put it in this code: if (hasBeenShaken && SJHShaking && buttonIsPressed(self.lastAction, acceleration, 0.7)) I get an error :/ – user1483652 Aug 08 '12 at 13:15
  • Should probably be `if(buttonIsPressed && hasBeenShaken && SJKSHaking(self.lastAction, acceleration, 0.7))` SJHShaking is a selector, so its parameters need to follow it. You also probably want to check buttonIsPressed first, since that will be false at the beginning of the application, and the rest of the if statement won't be run at all. – Liron Aug 08 '12 at 13:19
  • I tried what you said but it comes up with 2 error's:Undefined symbols for architecture armv7: "_SJKSHaking", referenced from: -[ViewControllerThree accelerometer:didAccelerate:] in ViewControllerThree.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation) – user1483652 Aug 08 '12 at 13:22
  • Okay so i did it again and its working, but now on my phone when i shake after pressing done nothing happens :( – user1483652 Aug 08 '12 at 13:27
  • Make sure that the buttonIsPressed is being set properly on the button press. Also, try and put a breakpoint at that if statement to see what the values are of everything when you do the shake. My guess is that hasBeenShaken is always false, so you are never getting the shake, but I don't know why it would have been shaking before either. – Liron Aug 08 '12 at 13:36
  • Sorry but I am really confused on what to do, i am a total begginer, do you want to move this to a chat? – user1483652 Aug 08 '12 at 13:37
  • StackOverflow doesn't have a built in chat, and I'd rather not give out my private information. Do you know how to set breakpoints in Xcode? – Liron Aug 08 '12 at 13:44
  • Dont worry i am now using motion ended which is much simpler :) thanks anyways – user1483652 Aug 08 '12 at 13:46
  • Before you do any coding at all, you should figure that out. Can't do anything without knowing how to debug. Google "xcode how to set breakpoint" – Liron Aug 08 '12 at 13:46
  • ah those blue arrows, yes I have used them okay thanks for you help so far :) – user1483652 Aug 08 '12 at 13:57
  • No problem. Can you mark my answer as accepted? (In general you should do this on all questions that you ask. People don't like helping people who don't repay the favor by at least clicking the checkbox.) – Liron Aug 08 '12 at 14:50