Motion Events IPHONE APPS

An iPhone or iPod touch device generates motion events when users move the device in a certain way, such as shaking it. Motion events have their origin in the device accelerometer. The system evaluates the accelerometer data and, if it meets certain criteria, interprets it as a gesture. It creates a UIEvent object representing this gesture and sends the event object to the currently active application for processing.

Motion events are much simpler than touch events. The system only tells an application when the motion starts and when it stops, and not when each individual motion occurs. Moreover, whereas a touch event includes a set of touches and their related state, a motion event carries with it no state other than the event type, event subtype, and timestamp. The system interprets motion gestures in such a way as to not conflict with orientation changes.

To handle motion events, a class inheriting from UI Responder must implement either the motion Began: withEvent: method or motionEnded: withEvent: method, or possibly both of these methods. For example, if an application wants to give horizontal shakes and vertical shakes different meanings, it could cache the current acceleration axis values in motion Began: with Event: compare those cached values to the same axis values in motion Ended: with Event: and act on the results accordingly. A responder should also implement the motion Cancelled: with Event: method to respond to events that the system sends to cancel a motion event; these events sometimes reflect the conclusion that the motion is not a valid gesture after all.

An application and its key window deliver a motion event to the window’s first responder for handling. If the first responder doesn’t handle it, the event progresses up the responder chain in a way similar to touch events until it is either handled or ignored. However, there is one important difference between touch events and shake motion events. When the user starts shaking the device, the system sends a motion event to the first responder in a motion Began: with Event: message; if the first responder doesn’t handle the event, it travels up the responder chain. If the shaking lasts less than a second or so, the system sends a motionEnded:withEvent: message to the first responder. But if the shaking lasts longer, or if the system determines the motion is not a shake, the first responder receives a motion Cancelled: with Event: message.

If a shake-motion event travels up the responder chain to the window without being handled, and the application Supports Shake To Edit property of UI Application is set to YES, iPhone OS displays a sheet with Undo and Redo commands. By default, this property is set to NO.