0

I want to create simple application on iphone. For this purpose i use accelerometer, my object is on the track and whenever it will go outside the track, it will detect if i moved iphone

The deals dealer
  • 1,006
  • 2
  • 8
  • 16

3 Answers3

1

Used Accelerometer reference from Apple. Thats really helpful to me.

Faraz Haider
  • 128
  • 9
0

See the UIAccelerometer class reference. There's sample code as well (see the Related sample code section near the top).

DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

copy and pasting directly from this stackoverflow answer

The APIs you are looking for are in UIResponder:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

Generally you just implement this:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (event.type == UIEventSubtypeMotionShake) {
    //Your code here
  }
}

in your UIViewController subclass (UIViewController is a subclass of UIResponder). Also, you want to handle it in motionEnded:withEvent:, not motionBegan:withEvent:. motionBegan:withEvent: is called when the phone suspects shaking is happening, but the OS can determine the difference between a user purposefully shaking, and incidental shaking (like walking up the stairs). If the OS decides it was not a real shake after motionBegan:withEvent: is called it will call motionCancelled: instead of motionEnded:withEvent:.

Community
  • 1
  • 1
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88