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”的方法未调用。我应该怎么做才能在我的应用程序中实现此功能?任何想法?
4 个解决方案
#1
59
This might help you...
https://*.com/a/2405692/796103
这可能会对你有帮助... https://*.com/a/2405692/796103
He says that you should set the UIApplication
's applicationSupportsShakeToEdit
to YES
. and override 3 methods in your VC:
他说你应该将UIApplication的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 :)
#2
26
You could do something like this...
你可以这样做......
Firstly...
首先...
Set the applicationSupportsShakeToEdit property in the App's Delegate:
在App的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方法添加到View Controller:
- (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:)
如果第一个答案没有,这应该工作,这只是快速键入未经测试:)
#3
13
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
#4
3
I extended UIApplication class and added class reference to main: MyApplication.h
我扩展了UIApplication类并添加了对main:MyApplication.h的类引用
@interface MyApplication : UIApplication
@end
MyApplication.m
MyApplication.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.
这适用于所有情况。
#1
59
This might help you...
https://*.com/a/2405692/796103
这可能会对你有帮助... https://*.com/a/2405692/796103
He says that you should set the UIApplication
's applicationSupportsShakeToEdit
to YES
. and override 3 methods in your VC:
他说你应该将UIApplication的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 :)
#2
26
You could do something like this...
你可以这样做......
Firstly...
首先...
Set the applicationSupportsShakeToEdit property in the App's Delegate:
在App的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方法添加到View Controller:
- (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:)
如果第一个答案没有,这应该工作,这只是快速键入未经测试:)
#3
13
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
#4
3
I extended UIApplication class and added class reference to main: MyApplication.h
我扩展了UIApplication类并添加了对main:MyApplication.h的类引用
@interface MyApplication : UIApplication
@end
MyApplication.m
MyApplication.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.
这适用于所有情况。