iOS:如何检测摇动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10154958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS : How to detect Shake motion?
提问by Samblg
I added the following code to my appDelegate.m
我将以下代码添加到我的 appDelegate.m
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake )
{
// User was shaking the device. Post a notification named "shake".
[[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)shakeSuccess
{
// do something...
}
and then I added :
然后我补充说:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// INIT THE BACKGROUND PATH STRING
[self refreshFields];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
***[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeSuccess) name:@"shake" object:nil];***
return YES;
}
When I start my app on my iPhone, the method named "shakeSuccess" doesn't called. What should I do to implement this function in my app? any idea?
当我在 iPhone 上启动我的应用程序时,没有调用名为“shakeSuccess”的方法。我应该怎么做才能在我的应用程序中实现这个功能?任何的想法?
回答by Christian Schnorr
This might help you...
https://stackoverflow.com/a/2405692/796103
这可能会帮助你...
https://stackoverflow.com/a/2405692/796103
He says that you should set the UIApplication's applicationSupportsShakeToEditto
YES. and override 3 methods in your VC:
他说你应该把UIApplication's设置applicationSupportsShakeToEdit为
YES。并覆盖 VC 中的 3 个方法:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
The rest of your code is fine. (the -motionEnded:withEvent:)
你的其余代码很好。( -motionEnded:withEvent:)
回答by Thomas Stone
You could do something like this...
你可以做这样的事情......
Firstly...
首先...
Set the applicationSupportsShakeToEdit property in the App's Delegate:
在应用程序的委托中设置 applicationSupportsShakeToEdit 属性:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.applicationSupportsShakeToEdit = YES;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
Secondly...
其次...
Add/Override canBecomeFirstResponder, viewDidAppear: and viewWillDisappear: methods in your View Controller:
在您的视图控制器中添加/覆盖 canBecomeFirstResponder、viewDidAppear: 和 viewWillDisappear: 方法:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
Thirdly...
第三...
Add the motionEnded method to your View Controller:
将 motionEnded 方法添加到您的视图控制器:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
// your code
}
}
That should work if the first answer did not and this is only quickly typed not tested:)
如果第一个答案没有,那应该可以工作,这只是快速输入未测试:)
回答by Cherpak Evgeny
If you want to be able to detect the shake motion across the app, the best way is to override the class of your application with a custom class.
如果您希望能够检测整个应用程序的抖动运动,最好的方法是使用自定义类覆盖应用程序的类。
And then implement this in your custom application class
然后在您的自定义应用程序类中实现它
@implementation PSApplication
- (void)sendEvent:(UIEvent *)event
{
if ( event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake ) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"shakeNotification" object:nil];
}
[super sendEvent:event];
}
@end
回答by Harit K
I extended UIApplication class and added class reference to main: MyApplication.h
我扩展了 UIApplication 类并添加了对 main: MyApplication.h 的类引用
@interface MyApplication : UIApplication
@end
MyApplication.m
我的应用程序.m
@implementation MyApplication
- (void) sendEvent:(UIEvent *)event
{
if( event && (event.subtype==UIEventSubtypeMotionShake))
{
AppDelegate *objAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[objAppDelegate doWhatEver];
[super sendEvent:event];
}
else
{
[super sendEvent:event];
}
}
@end
And the last step in main.m
以及 main.m 中的最后一步
int main(int argc, char *argv[])
{
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class]));
}
This works in all cases.
这适用于所有情况。

