2016-06-23 35 views
0

Ich möchte Device Shake mit CoreMotion-Framework für iOS 7 erkennen. Kann mir jemand helfen, wie dies zu tun?Erkennung von Shake-Geste durch CoreMotion

schrieb ich Code unten in meinem viewDidAppear beschrieben

CMMotionManager *motionManager = [[CMMotionManager alloc] init]; 
    motionManager.deviceMotionUpdateInterval = 1.0/60.0; 

__block double myAcceleration; 
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] 
            withHandler:^(CMDeviceMotion *motion, NSError *error) 
{ 
    myAcceleration = motion.userAcceleration.y; 
    CATransform3D transform; 
    transform = CATransform3DMakeRotation(
              motion.attitude.pitch, 1, 0, 0); 
    transform = CATransform3DRotate(transform, 
            motion.attitude.roll, 0, 1, 0); 
    transform = CATransform3DRotate(transform, 
            motion.attitude.yaw, 0, 0, 1); 
} 
]; 

aber nicht schütteln zu erkennen.

+0

Hey, bitte teilen Sie Ihre Antwort, wenn jemand über Shake in ios 7 und oben mit Kernbewegung wissen – Sudha

Antwort

3
#define accelerationThreshold 0.30 

- (void)motionMethod:(CMDeviceMotion *)deviceMotion 

{ 

CMMotionManager *motionManager = [[CMMotionManager alloc] init]; 

    CMAcceleration userAcceleration = deviceMotion.userAcceleration; 
    if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) 
    { 
     float sensitivity = 1; 
     float x1 = 0, x2 = 0, y1 = 0, y2 = 0, z1 = 0, z2 = 0; 

     double totalAccelerationInXY = sqrt(userAcceleration.x * userAcceleration.x + 
              userAcceleration.y * userAcceleration.y); 

     if (0.85 < totalAccelerationInXY < 3.45) { 
      x1 = userAcceleration.x; 
      y1 = userAcceleration.y; 
      z1 = userAcceleration.z; 

      float change = fabs(x1-x2+y1-y2+z1-z2); 
      if (sensitivity < change) { 

       // print change in position in coordinates. 
       NSLog (@"total=%f x=%f y=%f z=%f timeStamp:%f, UpTime:%f", totalAccelerationInXY, userAcceleration.x, userAcceleration.y, userAcceleration.z, deviceMotion.timestamp, [[NSProcessInfo processInfo] systemUptime]); 

       x2 = x1; 
       y2 = y1; 
       z2 = z1; 
      } 
     } 
    } 
} 
+0

Arbeitete für mich zu erkennen, schütteln von iOS-Geräten. – Sudha