2017-02-20 19 views
0

我试图复制类似于Runtastic's Fitness Apps的健身应用程序。使用CMDeviceMotion的仰卧起坐计数器

仰卧起坐

这我们第一次使用手机的内置加速计来检测移动应用程序。您需要将手机放在胸前,然后快速坐起来,足够高,以便加速计可以记录动作和应用程序以计算1次仰卧起坐。一定要做足够高的仰卧起坐!

我做了一个类似于这个问题的原型应用程序here,并试图实现一种计算仰卧起坐的方法。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    int count = 0; 

    motionManager = [[CMMotionManager alloc]init]; 

    if (motionManager.deviceMotionAvailable) 
    { 
     motionManager.deviceMotionUpdateInterval = 0.1; 

     [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) { 

      // Get the attitude of the device 
      CMAttitude *attitude = motion.attitude; 

      // Get the pitch (in radians) and convert to degrees. 
      double degree = attitude.pitch * 180.0/M_PI; 

      NSLog(@"%f", degree); 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       // Update some UI 

       if (degree >=75.0) 
       { 
        //it keeps counting if the condition is true! 
        count++; 
        self.lblCount.text = [NSString stringWithFormat:@"%i", count]; 
       } 
      }); 
     }]; 

     NSLog(@"Device motion started"); 
    }  
    else 
    { 
     NSLog(@"Device motion unavailable"); 
    } 
} 

if条件语句作品,如果我将设备放在我的胸口,并做适当的仰卧起坐,但关于这个问题,如果声明的是,它只会继续计数,我会希望它只有当设备回到原始位置时才算数。

任何人都可以拿出这个逻辑实现吗?

回答

0

一个简单的布尔标志的伎俩:

__block BOOL situp = NO; 


if (!situp) 
{ 
    if (degree >=75.0) 
    { 
     count++; 
     self.lblCount.text = [NSString stringWithFormat:@"%i", count]; 
     situp = YES; 
    } 
} 

else 
{ 
    if (degree <=10.0) 
    { 
     situp = NO; 
    } 
} 

不是最好的逻辑实现在这里,但它能够完成任务......