In the game, you are a motorcycle enthusiast, and now you are participating in a high-speed cycling competition. Unlike the usual ones, the track is held on the highway. If you avoid the passing vehicles as soon as possible, it is a big difficulty. Let us try it together!
Realization of gravity sensing
UIAccelerometer accelerometer is used to detect iphone mobile phone in the x.y.z axis acceleration on the three axes. To get this type of call:
UIAccelerometer * accelerometer = [UIAccelerometer sharedAccelerometer];
At the same time, you need to set its delegate.
UIAccelerometer * accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = 1.0 / 60.0;
The delegate method: - (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) The UIAcceleration in acceleration is the acceleration class. Contains really data from the accelerometer UIAccelerometer. It has 3 attributes x, y, z. iphone accelerometer support up to 100 times per second frequency polling. This time is 60 times.
1) The application can detect the shaking through the accelerometer, for example, the user can erase the drawing by shaking the iphone.
Users can also shake iphone several times in a row, the implementation of some special code:
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
static NSInteger shakeCount = 0;
static NSDate *shakeStart;
NSDate *now = [[NSDate alloc] init];
NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart];
if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil)
{
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
[now release];
[checkDate release];
if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0)
{
shakeCount++;
if (shakeCount > 4)
{
// -- DO Something
shakeCount = 0;
[shakeStart release];
shakeStart = [[NSDate alloc] init];
}
}
}