shaking on 3.0 in a UITableViewController
Asked Answered
K

2

0

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

Keefer answered 12/2, 2010 at 17:3 Comment(0)
R
1

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];
Reek answered 12/2, 2010 at 17:38 Comment(4)
This approach sounds great, and it's nice and very easy to implement. I'll give it a try! thanks, r.Keefer
I'm getting TWO calls from Notification Center when I Shake it on Simulator, but just ONCE on the device (iPhone) :-) thanks!Keefer
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 ?Keefer
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.Keefer
M
0

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

Matson answered 12/2, 2010 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.