我正在尝试在我的应用程序中执行一个方法,但只有在手机处于正确的加速度计方向一段时间后才能执行。我遇到的问题是每次更新加速度计时,计时器都会重置。我无法弄清楚如何只启动一次启动时间,而不是每次更新加速度计时都没有。
//在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
任何帮助都会很棒!
答案 0 :(得分: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
方法,这意味着计时器未添加到主runloop中。试试这个:
- (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
方法。