简体   繁体   中英

How the shake gesture works in iOS?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM