How To Detect Shake Motion Gesture On you iPhone or iPad

Today we are going just to detect Shake Motion gesture on iPhone or iPad.
We are going to create a new Universal application. Just create a new project, select single view and select “Universal” on devices option

Now you have a delegate and one “ViewController”.
Open the .xib file and place a UILabel of create a new UILabel programatically in your “viewDidLoad” method. Once we placed a UILabel the you may write some text init like “No Shake”

Now we have to write down some delegate methods of “Shake Gesture”.

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
// you can do any thing at this stage what ever you want. Change the song in playlist, show photo, change photo or whatever you want to do
    titleLabel.text = @"Shake Started";
}
 
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
	titleLabel.text = @"Shake Cancelled";
}
 
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
// you can do any thing at this stage what ever you want. Change the song in playlist, show photo, change photo or whatever you want to do
	titleLabel.text = @"Shake Ended";
}

That’s all now how you are going to use this thing, this is totally up to you. You may download the example project on this link ShakeGestureExample
Happy Coding and Mind Your Code :)

Leave a Reply

Your email address will not be published. Required fields are marked *