这段代码似乎没有给我任何输出,甚至没有设置加速度计。我首先尝试让设备识别加速度计和阈值输入。这是一个简单的加速度计应用程序,我可以在其中设置一个阈值保持变量,然后我可以比较之前和当前加速度计输出的一些值。thresh 输入是用于输入的简单文本框。任何帮助,将不胜感激。
#import "ViewController.h"
@interface ViewController (){
//set up variables for switch and BOOL variables
BOOL isStarted;
BOOL switchX;
BOOL switchY;
BOOL switchZ;
}
@end
@implementation ViewController
-(IBAction)startSensor:(id)sender{
//start with displaying the values...then move onto the just flash method
//set up the accelerometer function within the button press
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self showAccelerationdata:accelerometerData.acceleration];
//print statements for future debugging. only in the log files though
if (error){
NSLog(@"%@", error);
}
}];
}
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
currentAccelx = 0;
currentAccely = 0;
currentAccelz = 0;
previousAccelx = 0;
previousAccely = 0;
previousAccelz = 0;
//set up class for movement controls
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = .1;
}
-(IBAction)checkSwitchx:(UISwitch *)sender{
if (sender.on){
switchX = TRUE;
}
else{
switchX = FALSE;
}
}
-(IBAction)checkSwitchy:(UISwitch *)sender{
if (sender.on){
switchY = TRUE;
}
else{
switchY = FALSE;
}
}
-(IBAction)checkSwitchz:(UISwitch *)sender{
if (sender.on){
switchZ = TRUE;
}
else{
switchZ = FALSE;
}
}
-(void)showAccelerationdata:(CMAcceleration)acceleration
{
self.view.backgroundColor = [UIColor whiteColor];
if ([thresh.text length] == 0){
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"numbers are blank!" message:@"Please enter numbers!"
delegate:self cancelButtonTitle:@"Ok!" otherButtonTitles: nil];
[alert show];
[alert resignFirstResponder];
}
else{
threshold = [thresh.text floatValue];
NSLog(@"%@", thresh);
}
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end