This application will use the accelerometer data to offset the center of drawing in the HypnosisView. So, HypnosisView needs two properties to define the offset. In HypnosisView.h, add these properties.
@interface HypnosisView : UIView { float xShift, yShift; } @property (nonatomic, assign) float xShift; @property (nonatomic, assign) float yShift; @end
Now synthesize these properties in HypnosisView.m:
@implementation HypnosisView @synthesize xShift, yShift;
HypnosisView needs to know how to use these properties when it draws. In HypnosisView.m, add code to drawRect: to offset the center using the xShift and yShift instance variables:
// Draw concentric circles for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) { center.x += xShift; center.y += yShift; CGContextAddArc(context, center.x, center.y, currentRadius, 0, M_PI * 2.0, YES); CGContextStrokePath(context); }
Using the UIAcceleration object the accelerometer sends its delegate, set xShift and yShift and redraw the view. In HypnosisViewController.m, replace the following method:
- (void)accelerometer:(UIAccelerometer *)meter didAccelerate:(UIAcceleration *)accel { HypnosisView *hv = (HypnosisView *)[self view]; [hv setXShift:10.0 * [accel x]]; [hv setYShift:-10.0 * [accel y]]; // Redraw the view [hv setNeedsDisplay]; }
Build and run your application. The center of the view will move as you tilt the device. But the movement of the HypnosisView has a jerky feel that is not so conducive to hypnosis. We’ll fix that next.
3.135.247.181