ios - iOS:如何实现“震动设备”事件?

标签 ios shake

我想在我的应用中发生“摇晃设备”事件-即,当用户摇晃设备时,发生了一些事情。
我尝试实现:

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

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

最佳答案

尝试使用以下代码,对我来说效果很好。

- (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;
}

关于ios - iOS:如何实现“震动设备”事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6806937/

相关文章:

ios - 是否可以获取推送通知的发送时间戳?

ios - 摇晃一定时间?

jquery - 如何根据手机震动调用Ajax?

ios - 如何为 UITableViewCell 实现摇动动画

objective-c - 获取正确的 Node Json objective c

ios - Xamarin.iOS - 如何动态更改标签字体大小?

ios - 如何在不跳过的情况下在特定 anchor 旋转图层?

ios - 从开发者帐户中删除所有配置文件和证书