Few days ago, I saw an iPhone application, which as an wallpaper type application and the image was changed when iPhone shaked.
Then I try to learn how iPhone detects shaking.
In the following I describe it:
UIResponder class has two delegate method which is called when iPhone is shaked. The methods are:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
The first method Tells the receiver that a motion event has begun.
The second method Tells the receiver that a motion event has ended.
->Note:
1. iPhone OS informs the first responder only when a motion event starts and when it ends; for example, it
doesn’t report individual shakes.
2. The receiving
object must be the first responder to receive motion events .
-> How we can use this methods in our app:
The UIView which you want to respond to shake, you have to subclass this view of a UIView, and you have to override the 3 methods:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@" Shake Start");
/*
do what you want when shake start;
*/
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@" Shake End");
/*
do what you want when shake End;
*/
}
- (BOOL)canBecomeFirstResponder{ return YES; }
and in your view controller class, always respond that view as a first responder.
You can download my demo project on iPhone shaking from here
Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts
Sunday, January 17, 2010
Subscribe to:
Posts (Atom)