问题
I am trying to execute a method in my application, but only after the phone was been in the correct accelerometer orientation for a given time interval. The problem I am having is each time the accelerometer is updated, the timer resets. I have not been able to figure out how to trigger the time to start only once and not each time the accelerometer is updated.
// In 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
// In 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
Any assistance would be great!
回答1:
One way would be to insert a BOOL flag to indicate the timer has started, to prevent it constantly being unnecessarily re-started. You'd then reset the flag when the timer is invalidated. So you'd have something like this (not tested but I'm sure you'll get the gist):
- (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.");
}
}
EDIT
You're also calling your startTimer
method from a block, which mean the timer isn't being added to the main runloop. Try this instead:
- (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];
}
Your addTime
method should now get called correctly.
来源:https://stackoverflow.com/questions/17999375/accelerometer-must-be-in-a-specific-orientation-for-a-given-time-interval-before