在我的计步器应用程序中,为什么我的NSTimer在我想要它时不会停止?

时间:2010-07-03 14:47:27

标签: iphone objective-c

请帮帮我,朋友们。我已经完成了一个台阶计数器。我现在的问题是我的计时器从加速度计开始,但是当加速度计停止时它不会停止。(计步器是一个用户可以计算他/她的步数的应用程序。通过这些信息,用户可以计算他消耗的总卡路里,以及总耗用时间。)问题是我的计时器工作不正常。请帮帮我。

这是代码

if (  fabsf(acceleration.y) > progressAsInt ) {
        steps++;
        timer1 = [NSTimer scheduledTimerWithTimeInterval:(.01) target:self selector:@selector(timepassed) userInfo:nil repeats:YES];


        NSString *stepcount = [NSString stringWithFormat:@"%d", steps];
        [stepstaken setText:stepcount];

        //it's for the MPH speed calculation formula changes can be happen
        /*formula mph= miles/hour
         0.5miles
         10minutes = 10/60 hours = 0.1667
         mph=0.5/0.1667*/

        mspeed = (TotalDistance *3600)/60;
        //mspeed =(steps*steplength)/63360*60;
       NSString *speed = [NSString stringWithFormat:@"%2.2f",mspeed];
        [mSpeed setText:speed];


        //this for calculation of miles
        TotalDistance = (steps *steplength)/1500;
        NSString *distance =[NSString stringWithFormat:@"%5.3f",TotalDistance];
        [miles setText:distance];


       if (steps%20==0)
        {
            //And this for calorie calculation
            cal++;

            NSString *cal1 = [NSString stringWithFormat:@"%d",cal];
            [calorie setText:cal1];
        }else{
           [timer1 invalidate];

       }        
}

1 个答案:

答案 0 :(得分:0)

我修复了代码的缩进以匹配其结构。现在应该很明显,如果你试图在加速度低于阈值时使计时器失效,那么else位置错误。