objective c - detect when shakes an iPhone -


I have questions about finding the shake posted here, here's a warning:

"Now ... I wanted to do something similar (in the iPhone OS 3.0+), only in my case I wanted to make it an app-wide so that I can alert many parts of an action when there is a shake. What I'm doing.

First, I subclassed UIWindow, it's easy peasy to create a new class with an interface such as MotionWindow Create Ill.: Add a method like UIWindow (feel free to take your own, natch):

  - (zero) motionEnded: (UIEventSubtype) speed withEvent: (UIEvent *) Event {if (event.type == UIEventTypeMotion & amp; amp; event.subtype == UIEventSubtypeMotionShake} {[NSNotificationCenter defaultCenter] postNotificationName: @ "DeviceShaken" item: auto];}}  

Now, if you use a MainWindow.xib (Share Xcode template stuff), go there and change the class of your window object from UIWindow to MotionWindow or whatever you call it save to XB if you save the UISo Indo do programmatically set, instead use your new window class.

Your app is now using the special ii window class. Wherever you want to be told about shake, make notifications for them! Like this:

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

To remove yourself as a supervisor:

  remove [[NSNetification Center default center] to remove observer: auto];  

Question:

  1. Where to place notifications (I have a visual based app)?
  2. Do I have to remove myself?
  3. How can I know that Shake incident knows what it is "is already in progress?"

thanks.

iPhone OS 3.x The idea that the first responder is set as is as simple is

you see in class law override> motionEnded , like this:

  - (zero) motionEnded: (UIEventSubtype) speed withEvent: (UIEvent *) event {if (speed == UIEventSubtypeMotionShake & amp; IsViewLoaded]) // Handle Shake here ...}}  

In addition, you will need to become the first responder when the view loads and appears :

  - (zero) viewDidAppear: (BOOL) animated ([self-made First Responder]; [Super Viewedapper: Animated]; // Any additional set up code ...} / Code>  

You can canBecomeFirstResponder method The answer can also be.

  - (BUn) can be the first responder}  

used in any object Which gets the form UIView.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -