0

这段代码似乎没有给我任何输出,甚至没有设置加速度计。我首先尝试让设备识别加速度计和阈值输入。这是一个简单的加速度计应用程序,我可以在其中设置一个阈值保持变量,然后我可以比较之前和当前加速度计输出的一些值。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
4

2 回答 2

0

显示设置 self.motionManager 的代码。如果您实际上没有创建 CMMotionManager,您将在 startSensor: 方法中向 nil 发送消息,您将观察到没有执行加速度计回调。

于 2013-10-18T21:09:44.623 回答
0

我似乎没有正确设置我的 CMMotionManager。在我查看了我的设置之后,如何让所有内容正确显示非常简单。

于 2013-10-29T20:26:11.273 回答