1

我想给我的应用程序一个“摇动设备”事件——即,当用户摇动设备时,会发生一些事情。我尝试实施:

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

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

4

3 回答 3

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;
}
于 2013-11-13T07:23:51.817 回答
1

回答可能为时已晚,但在您的 viewDidLoad 中您需要包括在内。

[self becomeFirstResponder];

试试看。

于 2013-05-23T04:51:10.900 回答
0

除了 Taylor 的解决方案之外,还要确保您的 AppDelegate.m 中有这个。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationSupportsShakeToEdit = YES;
    return YES;
}
于 2013-10-13T07:46:24.203 回答