我想给我的应用程序一个“摇动设备”事件——即,当用户摇动设备时,会发生一些事情。我尝试实施:
-(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;
}
回答可能为时已晚,但在您的 viewDidLoad 中您需要包括在内。
[self becomeFirstResponder];
试试看。
除了 Taylor 的解决方案之外,还要确保您的 AppDelegate.m 中有这个。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
application.applicationSupportsShakeToEdit = YES;
return YES;
}