LAA
LAA

Reputation: 33

iPhone accelerometer changing senstivity - Cocoa Touch

Hello I want the sensitivity changing, for when the user moves the device. At the moment its not very sensitive, I believe its on default. I want it so its more sensitive, so when the user shakes the phone a little bit the sound plays.

Here is the code

Thanks

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self becomeFirstResponder];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(motion == UIEventSubtypeMotionShake)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"whip" ofType:@"wav"];
        if (theAudio) [theAudio release];
        NSError *error = nil;
        theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
        if (error)
            NSLog(@"%@",[error localizedDescription]);
        theAudio.delegate = self;
        [theAudio play];    
    }
}

Upvotes: 2

Views: 1875

Answers (3)

Todd Hopkinson
Todd Hopkinson

Reputation: 6903

First, make sure your interface adopts the UIAccelerobeterDelegate protocol.

@interface MainViewController : UIViewController <UIAccelerometerDelegate>

Now in your implementation:

//get the accelerometer
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = .1;
self.accelerometer.delegate = self;

Implement the delegate method:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{
  float x = acceleration.x;
  float y = acceleration.y;
  float b = acceleration.z;

  // here you can write simple change threshold logic
  // so you can call trigger your method if you detect the movement you're after
}

The values that the accelerometer returns for x,y, and z will always be a float between -1.0 and positive 1.0. You should call NSLog and output to the console x,y, and z values so you can get a feel for what they mean. Then you can develop a simple way to detect movement.

Upvotes: 4

Micah Hainline
Micah Hainline

Reputation: 14437

If you're trying to recreate the shake gesture, here are a few things to bear in mind:

A shake is a motion in a direction, followed by a motion in the generally opposite direction. That means you'll have to keep track of your previous acceleration vectors, so that you know when they change. So that you don't miss it, you'll have to sample from the accelerometer pretty frequently.

CGFloat samplesPerSecond = 30;
[[UIAccelerometer sharedAccelerometer] setDelegate: self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0 / samplesPerSecond]; 

Then in your delegate callback:

- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration {
  // TODO add this acceleration to a list of accelerations and keep the most recent (perhaps 30)
  // TODO analyze accelerations and determine if a direction change occurred
  // TODO if it did, then a shake occurred!  Clear the list of accelerations and perform your action
}

Upvotes: 0

Christian
Christian

Reputation: 1714

You cannot change how the shake events work. If you need something different you will have to write your own shake-detection code based on the x/y/z forces given to you by [UIAccelerometer sharedAccelerometer]; If you set the sharedAccelerometer's delegate to your class, you can get the accelerometer's accelerometer:didAccelerate: delegate callback.

Upvotes: 0

Related Questions