Thursday, October 6, 2011

Using the Accelerometer in an iOS FireMonkey app

Special thanks to Eugene Kryukov and Phil Hess for this stuff!

Check out CodeRage 6 for a cool implementation!

Enjoy!
implementation

{$R *.lfm}

type
UIAcceleration = objcclass external (NSObject)
public
function timestamp: NSTimeInterval; message 'timestamp';
function x: UIAccelerationValue; message 'x';
function y: UIAccelerationValue; message 'y';
function z: UIAccelerationValue; message 'z';
end;

AccDelegate = objcclass(NSObject)
procedure accelerometer_didAccelerate(accelerometer: UIAccelerometer; acceleration: UIAcceleration); message 'accelerometer:didAccelerate:';
end;

var
AccDelegatVar: AccDelegate;

procedure AccDelegate.accelerometer_didAccelerate(accelerometer: UIAccelerometer; acceleration: UIAcceleration);
begin
// Do whatever you want to with acceleration.x, y and z here!
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
UIAccelerometer.sharedAccelerometer.setUpdateInterval(1.0 / kAccelerometerFrequency);
AccDelegatVar := AccDelegate.alloc;
UIAccelerometer.sharedAccelerometer.setDelegate(AccDelegatVar);
end;

3 comments:

  1. Wouter van NifterickOctober 7, 2011 at 12:23 AM

    All these tips should be compiled into a wiki, the Delphi help, a book, or a code library or something.

    As soon as I start doing iOS stuff, I'll have to come back to look at all these tips. They look very helpful, but unfortunately I can't do anything with them yet.

    ReplyDelete
  2. Once they are fleshed out, I will do a better write up. For now I blog about the tips and tricks as I crack each one open. Also, CodeRage 6 during the week of October 17-21 will cover all of this.

    ReplyDelete
  3. You're actually a fantastic web marketer. The site reloading velocity will be wonderful. It would appear that you're performing virtually any unique technique free course. As well, This belongings are usually must-see. you could have carried out an excellent process with this subject!

    ReplyDelete