iOS:如何实现"摇动设备"事件?

Oha*_*gev 1 shake ios

我想在我的应用程序中使用"Shake Device"事件 - 即,当用户摇动设备时会发生一些事情.我试着实施:

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) {
        //something happens
    }
}
Run Code Online (Sandbox Code Playgroud)

它似乎不起作用.......
有谁知道我应该使用哪种方法?

abh*_*bhi 5

尝试使用下面的代码,它对我来说很好.

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
       {
          //your code
       }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
                [super motionEnded:motion withEvent:event];
}
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)