函數判斷是否shake了.
- static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
- double
- deltaX = fabs(last.x - current.x),
- deltaY = fabs(last.y - current.y),
- deltaZ = fabs(last.z - current.z);
- return
- (deltaX > threshold && deltaY > threshold) ||
- (deltaX > threshold && deltaZ > threshold) ||
- (deltaY > threshold && deltaZ > threshold);
- }
在shake探測對象中實現delegate方法
- //mark accelerometer delegate
- - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
- if (self.lastAcceleration) {
- /*histeresisExcited 表示是否已經在shake,避免過於頻繁的shake*/
- if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
- histeresisExcited = YES;
- /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */
- } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
- histeresisExcited = NO;
- }
- }
- self.lastAcceleration = acceleration;
- }

