iPhone Shake Detection using MotionEvent

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..... :)


Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList
Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen, Modified by Prasanna Vignesh.



About Me

Prasanna Vignesh Prasanna Vignesh
Mobile application developer at ZSL. More...



RecentPosts

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar