Each time the accelerometer sends an update, the center of the view changes to reflect the orientation of the device. However, because the accelerometer’s updateInterval is constant and the device’s movement is variable, the center of the view jumps around instead of moving in calm, peaceful, smooth manner. To get smooth movement, you need “smoothed” data. To smooth accelerometer data, you apply a low-pass filter.
In HypnosisViewController.m, add the following lines of code to filter the accelerometer data:
- (void)accelerometer:(UIAccelerometer *)meter didAccelerate:(UIAcceleration *)accel { HypnosisView *hv = (HypnosisView *)[self view]; float xShift = [hv xShift] * 0.8 + [accel x] * 2.0; float yShift = [hv yShift] * 0.8 - [accel y] * 2.0; [hv setXShift:xShift]; [hv setYShift:yShift]; // Redraw the view [hv setNeedsDisplay]; }
Build and run your application. The application will have a smoother response and a nicer feel.
3.15.221.133