Motion Events

Motion events are handled analogously to the way we handled touch events here. The motionBegan:withEvent: and motionEnded:withEvent: methods that class UIView inherits from class UIResponder are like the methods touchesBegan:withEvent: and touchesEnded:withEvent:. To detect a motion event, the responder must be the first responder.

Press control-command-z in the simulator to shake the iPhone for 0 seconds. Since this is too fast to see, we erase the message by calling the performSelector:withObject:afterDelay: method of class NSObject.

Source code in Motion.zip

  1. main.m
  2. Class MotionAppDelegate
  3. Class View

Things to try

  1. Examine the x, y, z accelerations while the motion event is in progress. When it ends, display their maximum values.