The accelerometer

The accelerometer detects the devices's real-world orientation by tracking the force of the earth's gravity on its X, Y, and Z axes. You can also use accelerometer data to detect changes in the device's velocity.

Setting up the accelerometer

To receive accelerometer data, your application needs to get hold of the application's shared instance of UIAccelerometer and give it an updateInterval and a delegate.

================design decision==============

If the HypnosisView is the accelerometer delegate, it becomes a self-contained object, which makes reusing it simpler. The problem is there can only be one accelerometer delegate. What if other objects need input from the accelerometer? HypnosisView can not forward information to other objects - it's not a controller. Therefore, the better option is to let HypnosisViewController be the delegate and receive the accelerometer updates.

================design decision==============

- (void)viewWillAppear:(BOOL) animated
{
    [super viewWillAppear:animated];
    UIAccelerometer *a = [UIAccelerometer sharedAccelerometer];

    //received updates every 0.1 second
    [a setUpdateInterval:0.1];
    [a setDelegate:self];
}

when view is moved off screen, you should set the accelerometer's delegate to nil.

- (void) viewWillDisappear: (BOOL) animated
{
    [super viewWillDisappear:animated];
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}

get accelerometer data

- (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];
}

 Detecting Shakes

the class UIResponder has been kind enough to implement methods that do math for you.

// Triggered when a shake is detected
- (void)motionBegan:(UIEventSubtype)motion
          withEvent:(UIEvent *)event;
// Triggered when the shake is complete
- (void)motionEnded:(UIEventSubtype)motion
          withEvent:(UIEvent *)event;
// Triggered when a shake is interrupted (by a call for example)
// Or if a shake lasts for more than a second
- (void)motionCancelled:(UIEventSubtype)motion
              withEvent:(UIEvent *)event;

 There is one more important detail: the window's firstResponder is the object that gets sent all of the motion events.

Right now, the first responder is not HypnosisView, but you can change that in two steps. First,

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

then,

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Monitoring accelerometer");
UIAccelerometer *a = [UIAccelerometer sharedAccelerometer];
[a setUpdateInterval:0.1];
[a setDelegate:self];
[[self view] becomeFirstResponder];
}

In general, there are two ways of altering the accelerometer data to suit your needs: change the frequency of accelerometer data updates and apply a filter to the data. When you are writing an application that relies on accelerometer data, you should determine the update interval and filtering algorithm that gives the user the best experiences.

 Retina

In Core Graphics, also called Quartz, we describe lines, curves, text, etc. in terms of points. On a non-Retina display, a point
is 1x1 pixels. On a Retina display, a point is 2x2 pixels.

All you have to do is suffix the higher-resolution image with @2x. like Time@2x.png

 

 

posted on 2012-06-15 19:17  grep  阅读(209)  评论(0编辑  收藏  举报