Question

I'm trying to change my UIAcceleromter's updateInterval via a UISlider that I have in my .xib. My NSLogs shows me the values perfectly, but the acceleromter's interval does not increase in speed when I move the slider. I'm not sure what's wrong.

I appreciate any help offered. Here is my code:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAccelerometerDelegate> {

}

- (IBAction)sliderValueChanged:(UISlider *)slider;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize XAxis, YAxis, delta;

float value;

- (void)viewWillAppear:(BOOL)animated
{   
    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;
    accel.updateInterval = 1.0f * value;

    NSLog(@"Value: %f",value);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    NSLog(@"X: %g",acceleration.x);
    NSLog(@"Y: %g",acceleration.y);
    NSLog(@"Z: %g",acceleration.z);
}

- (IBAction) sliderValueChanged:(UISlider *)slider 
{  
    value = slider.value;
    NSLog(@"Value: %f",slider.value);
} 

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

@end
Was it helpful?

Solution

There isn't a line of code that changes you accelerometer's update interval. All you are doing is remembering the value of the slider in a float and outputting it via NSLog.

The accelerometers value is only being set when your view appears.

You need to store the accelerometer as a property of your object and do something like

self.accelerometer.updateInterval = value;

in your sliderValueChanged: method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top