2013-10-02 46 views
0

如何設置的處理,所以我可以當裝置靜止不動(在某些閾值)檢測當裝置靜止不動CMMotionManager檢測

如預期下面的代碼不工作(該userAcceleration只是磨碎機取決於設備的方向,並且用戶甚至不移動設備(該值可能來自重力))

self.motionManager = [[CMMotionManager alloc] init]; 
     self.motionManager.accelerometerUpdateInterval = 1/2.0; 
     [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 

      float accelerationThreshold = 0.75; 
      CMAcceleration userAcceleration = accelerometerData.acceleration; 
      if ((fabs(userAcceleration.x) > accelerationThreshold) 
       || (fabs(userAcceleration.y) > accelerationThreshold) 
       || (fabs(userAcceleration.z) > accelerationThreshold)) { 
       self.deviceMoved = YES; 
      } else { 
       self.deviceMoved = NO; 
      } 

     }]; 

回答

1

使用startDeviceMotionUpdatesToQueue

if (self.motionManager == nil) { 
     self.motionManager = [[CMMotionManager alloc] init]; 
     self.motionManager.accelerometerUpdateInterval = 1/2.0; 
     [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) { 

      float accelerationThreshold = 0.25; 
      CMAcceleration userAcceleration = motion.userAcceleration; 
      if ((fabs(userAcceleration.x) > accelerationThreshold) 
       || (fabs(userAcceleration.y) > accelerationThreshold) 
       || (fabs(userAcceleration.z) > accelerationThreshold)) { 
       self.deviceMoved = YES; 
      } else { 
       self.deviceMoved = NO; 
      } 

     }]; 
    } 
需要