Question

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.

Was it helpful?

Solution

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];

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top