0

如何设置处理,以便我可以检测设备何时静止(在某个阈值中)

下面的代码没有按预期工作(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;
            }

        }];
4

1 回答 1

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;
            }

        }];
    }
于 2013-10-02T21:30:15.083 回答