0

I know how to detect shake gesture. It can be detect by following procedure :

Open your 'AppDelegate.m' and add the code below to the application:didFinishLaunchingWithOptions: method.

You must add it before the 'return YES'.

[application setApplicationSupportsShakeToEdit:YES];

Now open your 'viewController.m' file of the controller in which you want to use the shake gesture. Edit the viewDidAppear: and viewDidDisappear: methods.

Add the following code:

- (void)viewDidAppear:(BOOL)animated {

    [self becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {

    [self becomeFirstResponder];
}

After that you should add the methods in the code below:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    // Do your thing after shaking device
}

But I wanted to know logic how shake works.

How it can be detected? Does it use CoreMotion (accelerometer, magnetometer, and the gyroscope) for detecting?

If anyone knows please share.

Irfan
  • 4,301
  • 6
  • 29
  • 46
Bhushan B
  • 2,470
  • 4
  • 26
  • 38
  • I'd say it uses the accelerometer to take measurements and based on this it calculates whether its a shake or just moving in one direction only. – Robert J. Clegg Apr 14 '14 at 05:55
  • 1
    Possibly this might help you: http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone/2405692#2405692 – pankaj asudani Apr 14 '14 at 05:56
  • This link may help for understanding accelerometer,gyroscope.http://gizmodo.com/the-iphone-5s-motion-sensors-are-totally-screwed-up-1440286727 and also check this link http://nscookbook.com/2013/03/ios-programming-recipe-19-using-core-motion-to-access-gyro-and-accelerometer/ – Jay Mehta Apr 14 '14 at 07:53

1 Answers1

0

it depends on what device you have, but Core Motion is indeed the correct answer.

Devices usually have two ways to go about detecting motion, the accelerometer and the gyroscope.
Not all iOS devices have a gyroscope though. Regardless, Core Motion when enabled tracks the device and allows you to sample from one or both.

Check out apple's docs for a more detailed understanding.

flooie
  • 89
  • 2
  • 8