/**
Module Name: UIAccelerometer
Description: Explain how to use UIAccelerometer
Author: Su XinDe
Created: 1:06 12/27/2011
Last Change: 1:06 12/27/2011
*/
//UIAccelerometer
- (void)viewDidLoad
{
//......
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.undateInterval = 1.0f / 60.0f; //tiem interval , 60 times per second, the max support value is 100 times per second
//......
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceletration *)acceleration
{
//Log the coordinate value changing when you shake the phone
//NSString *numberOfShake = [[NSStirng alloc] initWithFormat:@"x:%g\ty:%g\tz:%g",acceleration.x,acceleration.y,acceleration.z];
//shakeValueLabel.text = numberOfShake;
//[numberOfShake release];
//inspecting the shaking, and do the thing you want the phone to do
if(fabsf(acceleration.x)>2.0||fabsf(acceleration.y>2.0)||fabsf(acceleration.z)>2.0)
{
// do the operation you want to do
//just like
//NSLog(@"Shaking movement inspected");
}
}
参考:
http://hi.baidu.com/suxinde2009/item/60f003ebacb4cde9fb42ba53