I added the following code to my 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?
Just shake your phone to undo. While the Mac has Command-Z, the iPhone has its own unique way of fixing typing mistakes: Shake to Undo. Shaking your device to go back or undo a mistake has been around since 2009 and iOS 3 (called iPhone OS back then). And it's one of the most overlooked features on iOS.
This might help you...
https://stackoverflow.com/a/2405692/796103
He says that you should set the UIApplication
's applicationSupportsShakeToEdit
to
YES
. and override 3 methods in your VC:
-(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:
)
You could do something like this...
Firstly...
Set the applicationSupportsShakeToEdit property in the App's Delegate:
- (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:
-(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:
- (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:)
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
I extended UIApplication class and added class reference to main: MyApplication.h
@interface MyApplication : UIApplication
@end
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
int main(int argc, char *argv[])
{
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class]));
}
This works in all cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With