I had a lot of problems for this to work, and I finally gave up and followed the advice of jandrea. He proposed to subclass UIWindow and implement the movement indicated there. This is a quote from his post here , look for him quite far.
First, I subclassed UIWindow. It is very simple. Create a new class file with an interface such as MotionWindow: UIWindow (feel free to choose your own, natch). Add a method like this:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) { [[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceShaken" object:self]; } }
Change @ "DeviceShaken" to the notification name of your choice. Save file.
Now, if you use MainWindow.xib (material for the Xcode template), go in there and change the class of your Window Object from UIWindow to MotionWindow or whatever you call It. Save xib. If you configured UIWindow programmatically, use the new Window class instead.
Your application now uses the specialized UIWindow Class. Wherever you want to talk about shaking, register to notify them! Like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceShaken) name:@"DeviceShaken" object:nil];
To remove yourself as an observer:
[[NSNotificationCenter defaultCenter] removeObserver:self];
willcodejavaforfood
source share