tailieunhanh - Lập trình iPhone part 15

Whee! Một trong những tính năng thú vị nhất của iPhone và iPod Touch được tích hợp gia tốc, thiết bị nhỏ bé mà cho phép iPhone biết làm thế nào nó được tổ chức và nếu nó được di chuyển. Các hệ điều hành iPhone người sử dụng gia tốc để xử lý xoay tự động, và nhiều trò chơi sử dụng như là cơ chế kiểm soát của nó. | Chapter 15 Whee ne of the coolest features of the iPhone and iPod Touch is the built-in accelerometer the tiny device that lets the iPhone know how it s being held and if it s being moved. The iPhone OS uses the accelerometer to handle autorotation and many games use it as a control mechanism. It can also be used to detect shakes and other sudden movement. Accelerometer Physics An accelerometer measures both acceleration and gravity by sensing the amount of inertial force in a given direction. The accelerometer inside iPhone is a three-axis accelerometer meaning that it is capable of detecting either movement or the pull of gravity in three-dimensional space. As a result you can use the accelerometer to tell not only how the phone is currently being held as autorotation does but also if it s laying on a table and even whether it s face down or face up. Accelerometers give measurements in g-forces g for gravity so a value of returned by the accelerometer means that 1 g is sensed in a particular direction. If the iPhone is being held still with no movement there will be approximately 1 g of force exerted on it by the pull of the earth. If the iPhone is being held upright in portrait orientation the iPhone will detect and report about 1 g of force exerted on its y axis. If the iPhone is being held at an angle that 1 g of force will be distributed along different axes depending on how the iPhone is being held. When held at a 45-degree angle that 1 g of force will be split roughly equally between two of the axes. Sudden movement can be detected by looking for accelerometer values considerably larger than 1 g. In normal usage the accelerometer does not detect significantly more than 1 g on any axis. If you shake drop or throw your 441 442 CHAPTER 15 Whee iPhone the accelerometer will detect a greater amount of force on one or more axes. Please do not drop or throw your own iPhone just to test this theory. You can see a graphic representation of the three axes used by