Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically trigger shake event iOS

How can I programmatically trigger the shake event in iOS?

I've tried the following but it keeps crashing...

+ (void)shake {
    NSLog(@"TEST");

    UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];

    m->_subtype = UIEventSubtypeMotionShake;
    m->_shakeState = 1;

    [[[UIApplication sharedApplication] keyWindow] motionBegan:UIEventSubtypeMotionShake withEvent:m];
    [[[UIApplication sharedApplication] keyWindow] motionEnded:UIEventSubtypeMotionShake withEvent:m];
}

What does apple do in the simulator under Hardware > Shake Gesture?

like image 870
user3558410 Avatar asked Dec 04 '25 01:12

user3558410


1 Answers

Try to replace

UIMotionEventProxy *m = [[NSClassFromString(@"UIMotionEvent") alloc] _init];

with

UIMotionEventProxy *m = [[UIMotionEventProxy alloc] _init];

I guess it's crushing when NSClassFromString(@"UIMotionEvent") returns nil.

like image 108
Mark Pervovskiy Avatar answered Dec 05 '25 14:12

Mark Pervovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!