2017-02-20 9 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"); 
    } 
} 

文はそれだけでカウントを継続し、私はそれをしたいということであれば、条件文は、私は私の胸にデバイスを配置し、適切なシットアップを行うかのように、動作しますが、この程度の問題ならばザ・デバイスが元の位置に戻ったときだけカウントされます。

誰でもこれを論理的に実装できますか?

答えて

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

ないここで、最良の論理的な実装を、それは仕事を取得します...

関連する問題