On the shake of the iPhone device i want some function to be called, i dont know how to recognize shake so i tried some link and tried this code
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.type == UIEventSubtypeMotionShake) { NSLog(@"called"); [self.view setBackgroundColor:[UIColor greenColor]]; } } - (BOOL)canBecomeFirstResponder { return YES; }
But alas no luck so can you please let me know how i can do the same
Thanks and Regards
Go to the ViewController.add the following method. If the motion is an Shake Gesture, then the Label text is updated. Build and Run the project and shake the device. Note with the iOS Simulator you can select the Shake Gesture in the Hardware menu.
Shake to Undo is a decade-old feature in iOS that allows you to erase your previous actions by simply shaking your iPhone. Some people may find this convenient, while some may find it bothersome. There's a simple way to toggle this feature on or off in your Settings under Accessibility.
As per the above comments discussed i am answering my question so that others can read the solution that i have got
In the UIResponder class documentation it is said that the motion events will respond to the first responder only so what i did was add just a small function and that did the trick for me, so here's my solution
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.type == UIEventSubtypeMotionShake) { NSLog(@"called"); [self.view setBackgroundColor:[UIColor greenColor]]; } } - (BOOL)canBecomeFirstResponder { return YES; }
Now still i was not able to detect any shake motion so all i had to do was to make my viewcontroller the first responder and for that here's the code that i used
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; }
and i was done
This was the solution that i came up with
Thanks and Regards
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 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