Reputation: 2378
I use below code for accelerometer and it works on ipod 3g but doesn't work on iphone4 (doesn't show alert)
.h
#define kAccelerationThreshold 2.2
#define kUpdateInterval (1.0f/10.0f)
.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIAccelerometer *accel=[UIAccelerometer sharedAccelerometer];
accel.delegate=self;
accel.updateInterval=kUpdateInterval;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Shake" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
Upvotes: 0
Views: 585
Reputation: 4696
Copied your code into a new project - in order to get it to work I had to lower the kAccelerationThreshold
value to around 1.5 - which still requires a fairly violent shake.
Only tested it on iPhone 4.
Upvotes: 1
Reputation: 2226
accel=updateInterval=kUpdateInterval;
Looks like it should be
accel.updateInterval=kUpdateInterval;
Upvotes: 0
Reputation: 50727
Check out the UIAccelerometer Class Reference.
Be sure to also implement the UIAccelerometerDelegate
protocol in your header.
Upvotes: 0