by Prasanna Vignesh
17. June 2010 13:15
Two simple way to detect the shake of iPhone,
1. Using UIAccelerometer.
2. Using Motion Events.
Using UIAccelerometer
Check this Link.
Using Motion Events
In your UIViewController add the following methods.
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (event.type == UIEventTypeMotion && event.subtype==UIEventSubtypeMotionShake) {
//Your code here
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (event.type == UIEventTypeMotion && event.subtype==UIEventSubtypeMotionShake) {
//Your code here
}
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (event.type == UIEventTypeMotion && event.subtype==UIEventSubtypeMotionShake) {
//Your code here
}
}
Add this method.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
You can also write it in viewWillAppear and viewWillDisappear
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];
}
Run the app in device. Shake it..... :)