Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Detecting a shake

I'm using the shake api like this:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        [img stopAnimating];    
    }
}

How do I detect that the shaking has stopped?

like image 660
Chris Avatar asked Jan 26 '11 17:01

Chris


People also ask

How to detect shake in iOS?

Go to the ViewController.add the following method. If the motion is an Shake Gesture, then the Label text is updated. Build and Run the project and shake the device. Note with the iOS Simulator you can select the Shake Gesture in the Hardware menu.

How to shake iPhone simulator?

In the Hardware menu, choose Shake Gesture. That's it. Your shake handler should be called when you do so.


1 Answers

You are on the right track, however, there are still more things you need to add to detect shaking:

You can test this by adding an NSLog to the motionBegan or motionEnded methods, and in the Simulator, press CONTROL + COMMAND + Z

#pragma mark - Shake Functions

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:NO];
    [self becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:NO];
}

-(void)viewDidDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewDidDisappear:NO];
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake )
    {
        // shaking has began.   
    }   
}


-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake )
    {
        // shaking has ended
    }
}
like image 93
WrightsCS Avatar answered Oct 14 '22 13:10

WrightsCS