tags:

views:

192

answers:

2

Hello,

after reading some posts about implementing shaking on 3.0, I think I get the idea but I'm not getting any call to the:

motionBegan motionEnded motionCancelled

this is an example of what I've read: http://stackoverflow.com/questions/1340492/how-to-detect-and-program-around-shakes-for-the-iphone

I'm sure I've added the

[self becomeFirstResponder];

and the

-(BOOL)canBecomeFirstResponder {
NSLog(@"First responder");
return YES;
}

Should I enable a special delegate for those events ?

I understand that those events are controlled by the system, and they are passed to the first responder, and go on ...

any idea ?

thanks,

r.

A: 

Where do you call becomeFirstResponder? You should do it in viewDidAppear. Does this get fired?

Felix
Yes, I call in viewDidAppear, and yes, it's fired. thanks
mongeta
+1  A: 

I had loads of problems getting this to work and I finally gave up and followed jandrea's advice. He suggested subclassing UIWindow and implement the motionEnded there. This is a quote from his post here, look for it quite far down.

First, I subclassed UIWindow. This is easy peasy. Create a new class file with an interface such as MotionWindow : UIWindow (feel free to pick your own, natch). Add a method like so:

- (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 the file.

Now, if you use a MainWindow.xib (stock Xcode template stuff), go in there and change the class of your Window object from UIWindow to MotionWindow or whatever you called it. Save the xib. If you set up UIWindow programmatically, use your new Window class there instead.

Now your app is using the specialized UIWindow class. Wherever you want to be told about a shake, sign up for them notifications! Like this:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceShaken) name:@"DeviceShaken" object:nil];

To remove yourself as an observer:

[[NSNotificationCenter defaultCenter] removeObserver:self];
willcodejavaforfood
This approach sounds great, and it's nice and very easy to implement. I'll give it a try!thanks,r.
mongeta
I'm getting TWO calls from Notification Center when I Shake it on Simulator, but just ONCE on the device (iPhone) :-) thanks!
mongeta
no problem mate :)
willcodejavaforfood
mmmmm, now I'm getting also TWO calls from notification center on my device (iPhone) ... should I remove the notification once I get the first one, and later activate it if needed ?
mongeta
yes, this was the problem, If I shake too much I get too many 'calls', disabling at the first call and activating it later works perfect ...thanks,r.
mongeta