0

我试图在我的应用程序中执行一个方法,但只有在手机在给定的时间间隔内处于正确的加速度计方向之后。我遇到的问题是每次更新加速度计时,计时器都会重置。我无法弄清楚如何触发仅启动一次的时间,而不是每次更新加速度计时。

// 在 ViewController.h 中

#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>

@interface ViewController : UIViewController
{
    float timeTest;
}

@property (nonatomic, strong) CMMotionManager *motionManager;
@property (nonatomic, strong) NSTimer *timerObject;

- (void) testMethod;

- (void) addTime;

- (void) startTimer;

@end

// 在 ViewController.m 中

#import "ViewController.h"

@implementation ViewController

@synthesize motionManager;
@synthesize timerObject;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.motionManager = [[CMMotionManager alloc]init];
    [self testMethod];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) testMethod
{
    if ([self.motionManager isAccelerometerAvailable])
    {
        NSOperationQueue *queue = [[NSOperationQueue alloc]init];
        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData,NSError *error)
         {
             if (ABS(accelerometerData.acceleration.x) < 0.30 && ABS(accelerometerData.acceleration.y) < 0.30 && ABS(accelerometerData.acceleration.z) > 0.70) // Phone is flat and screen faces up
             {
                 NSLog(@"Correct position.");  
                 [self startTimer]; // Problem here.  This just continually resets the timer for each accelerometer update.
             }
             else
             {
                 NSLog(@"Incorrect position.");
                 [timerObject invalidate];
                 timeTest = 0.0;
            }

         }];
    }
    else
    {
        NSLog(@"Accelerometer is not available.");
    }
}


- (void) addTime
{
    timeTest = timeTest + 0.1;
    if (timeTest == 3.0)
    {
        [timerObject invalidate];

        // Where code for next action would go.  For feedback, just simply set the screen to green.
        self.view.backgroundColor = [UIColor greenColor];
    }
}

- (void) startTimer
{
    timeTest = 0.0;
    timerObject = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(addTime) userInfo:nil repeats:YES];
}

@end

任何帮助都会很棒!

4

1 回答 1

0

一种方法是插入一个 BOOL 标志来指示计时器已启动,以防止它不断地不必要地重新启动。然后,当计时器无效时,您将重置标志。所以你会有这样的东西(未经测试,但我相信你会明白要点):

- (void) testMethod
{
    BOOL timerStarted = NO;
    if ([self.motionManager isAccelerometerAvailable])
    {
        NSOperationQueue *queue = [[NSOperationQueue alloc]init];
        [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData,NSError *error)
         {
             if (ABS(accelerometerData.acceleration.x) < 0.30 && ABS(accelerometerData.acceleration.y) < 0.30 && ABS(accelerometerData.acceleration.z) > 0.70) // Phone is flat and screen faces up
             {
                 NSLog(@"Correct position."); 
                 if(timerStarted == NO){//Only start the timer if it's not already running
                     [self startTimer]; 
                     timerStarted = YES;
                 }
             }
             else
             {
                 NSLog(@"Incorrect position.");
                 [timerObject invalidate];
                 timerStarted = NO;
                 timeTest = 0.0;
            }

         }];
    }
    else
    {
        NSLog(@"Accelerometer is not available.");
    }
}

编辑 您还startTimer从一个块中调用您的方法,这意味着计时器没有被添加到主运行循环中。试试这个:

- (void) startTimer
{
    timeTest = 0.0;
    self.timerObject = [NSTimer timerWithTimeInterval:0.1f target:self selector:@selector(addTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timerObject forMode:NSDefaultRunLoopMode];
}

您的addTime方法现在应该被正确调用。

于 2013-08-01T16:47:25.727 回答