shaking on 3.0 in a UITableViewController

安稳与你 提交于 2019-12-12 04:07:02

问题


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.


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



回答2:


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



来源:https://stackoverflow.com/questions/2253574/shaking-on-3-0-in-a-uitableviewcontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!