Ios 使用CMDeviceMotion的仰卧起坐计数器
我正在尝试复制一个类似于的健身应用程序 仰卧起坐 这是我们第一款使用手机内置加速计检测运动的应用程序。你需要将手机靠在胸前,然后快速坐起来,坐得足够高,让加速度计记录移动,让应用程序数到1。一定要做一个适当的仰卧起坐去足够高 我做了一个类似于这个问题的原型应用程序,并试图实现一种计算仰卧起坐的方法Ios 使用CMDeviceMotion的仰卧起坐计数器,ios,objective-c,accelerometer,core-motion,Ios,Objective C,Accelerometer,Core Motion,我正在尝试复制一个类似于的健身应用程序 仰卧起坐 这是我们第一款使用手机内置加速计检测运动的应用程序。你需要将手机靠在胸前,然后快速坐起来,坐得足够高,让加速度计记录移动,让应用程序数到1。一定要做一个适当的仰卧起坐去足够高 我做了一个类似于这个问题的原型应用程序,并试图实现一种计算仰卧起坐的方法 - (void)viewDidLoad { [super viewDidLoad]; int count = 0; motionManager = [[CMMoti
- (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条件语句起作用,就好像我把设备放在胸前,做了一个适当的仰卧起坐,但if语句的问题是,它将继续计数,我希望它只在设备回到原始位置时计数
有人能想出一个逻辑实现吗?一个简单的布尔标志实现了这一点:
__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;
}
}
\uuu块BOOL situp=否;
如果(!仰卧起坐)
{
如果(度>=75.0)
{
计数++;
self.lblCount.text=[NSString stringWithFormat:@“%i”,count];
仰卧起坐=是;
}
}
其他的
{
如果(学位)